I have the following query and it is not working as expected.
$students = StudentStatus::with(['user.studentProgramme' => function ($query) {
$query->where('department_course_id', request('studyCourse'));
}], 'level', 'user.studentProgramme.course')
->where('level_id', request('level'))
->where('status', 0)
->get();
The inner WHERE, that is $query->where('department_course_id', request('studyCourse')) is ignored and I don't know why.
Is there something I am missing out?
This may be a scope issue try:
$studyCourse = ;
$students = StudentStatus::with(['user' => function($query) {
$query->with(['studentProgramme' => function ($query) {
$query->where('department_course_id', request('studyCourse'));
}]}], 'level', 'user.studentProgramme.course')
->where('level_id', request('level'))
->where('status', 0)
->get();
Related
I'm trying to customize a filter in sonata admin that returns all the objects that have null on one specific field... In MySQL, there was a function that I used to get the null values which were the is null.
I'm trying to do the same but using mongo this time it gives an error that the isnull function is not defined. Is there any function similar to that in the form to use in odm?
My code is the following:
->add("isRoot", 'doctrine_mongo_callback', array(
'callback' => function ($queryBuilder, $alias, $field, $value) {
/**
* #var QueryBuilder $queryBuilder
*/
if ($value['value']) {
if ($value['value'] == 0) {
$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));
return true;
} else {
$category = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb.odm.document_manager')->getReference(ArticleCategory::class, $value['value']);
$queryBuilder->andWhere($queryBuilder->expr()->eq($alias.'.category', $category));
return true;
}
}
},
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => $this->getCategoryChoices()
),
'label' => 'mainCategory'
));
}
Is there a function in ODM similar to isNull in ORM?
the following is the error:
Attempted to call an undefined method named "isNull" of class "Doctrine\ODM\MongoDB\Query\Expr".
So I figured how to query instead of
$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));
Using the following:
$queryBuilder->field('mainCategory')->equals(null);
I saw this solution on the following link: enter link description here
Greeting everyone and especially to Senior of CodeIgniter Developer.
I have a problem with website that was built from CodeIgniter (I am not the developer inherited by previous epmployer). This website cannot working properly, especially when upload the image and shows warning about
Error Number: 1048 Column 'article_image' cannot be null
I did try with find the problem on the script and database, but nothing change and because no one change the codes & contents. Today, i try again with changing the "Null into yes" (previously is "No"), and tried to upload the article. It is miracle that it is working but the next problem is the image is gone (broken). I search at other and looking people with same problem with me says that i need to upgrade the CodeIgniter. Mine is 3.0.0 while the latest is 3.1.10. I copy paste the content inside /System and /views/error/cli not making better but make it worse, the image at the web page is gone(missing).
This is my Article_model
defined('BASEPATH') OR exit('No direct script access allowed');
class Article_model extends CI_Model {
public function get_all()
{
// $this->db->order_by('article_date', 'desc');
// $this->db->order_by('article_view', 'desc');
// $query = $this->db->get_where('article', array('flag !=' => 3));
// return $query->result_array();
$query = $this->db->query("SELECT A.*,B.*, A.id AS id_article FROM article AS A JOIN category B ON A.article_category = B.id WHERE A.flag != 3 ORDER BY article_date DESC, article_view DESC ");
return $query->result_array();
}
public function check($article_id)
{
$query = $this->db->get_where('article', array('flag !=' => 3, 'id' => $article_id));
return $query->row_array();
}
public function get_category()
{
$query = $this->db->order_by('category_name', 'ASC')->get_where('category', array('flag' => 1));
return $query->result_array();
}
public function get_tag()
{
$query = $this->db->order_by('tag_name', 'ASC')->get_where('tag', array('flag' => 1));
return $query->result_array();
}
public function get_selected_tag($article_id)
{
$query = $this->db
->select('tag.id, tag_name')
->join('article_tag', 'tag_id = tag.id')
->where(array('tag.flag' => 1, 'article_id' => $article_id))
->get('tag');
return $query->result_array();
}
public function insert()
{
$this->load->helper('upload');
$this->load->library('image_moo');
$image = file_upload('article_image', 'upload/article');
$this->image_moo->load($image['full_path']);
$this->image_moo->resize(924, 527)->save($image['path'] . '/' . $image['file_name'], TRUE);
$this->image_moo->resize_crop(100, 69)->save($image['path'] . '/thumb/' . $image['file_name']);
$this->image_moo->resize_crop(367, 232)->save($image['path'] . '/cover/' . $image['file_name']);
$insert_data = array(
'article_author' => $this->session->admin_id,
'article_title' => $this->input->post('article_title'),
'article_slug' => url_title($this->input->post('article_title'), '-', TRUE),
'article_category' => $this->input->post('article_category'),
'article_image' => $image['file_name'],
'article_content' => $this->input->post('article_content'),
'is_popup' => $this->input->post('is_poup'),
'article_date' => $this->input->post('article_date'),
);
$this->db->insert('article', $insert_data);
$article_id = $this->db->insert_id();
foreach ($this->input->post('article_tag') as $tag)
{
// Check apakah tag itu udah ada di database?
if (is_numeric($tag))
{
$tag_id = $tag;
}
else
{
$this->db->insert('tag', array('tag_name' => strtolower(trim($tag)), 'tag_slug' => url_title(trim($tag), '-', TRUE)));
$tag_id = $this->db->insert_id();
}
if ( ! $this->db->get_where('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id))->row_array())
{
$this->db->insert('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id));
}
}
}
public function update($id)
{
$this->load->helper('upload');
$this->load->library('image_moo');
$image = file_upload('article_image', 'upload/article');
$this->image_moo->load($image['full_path']);
$this->image_moo->resize(924, 527)->save($image['path'] . '/' . $image['file_name'], TRUE);
$this->image_moo->resize_crop(100, 69)->save($image['path'] . '/thumb/' . $image['file_name']);
$this->image_moo->resize_crop(367, 232)->save($image['path'] . '/cover/' . $image['file_name']);
$insert_data = array(
'article_author' => $this->session->admin_id,
'article_title' => $this->input->post('article_title'),
'article_slug' => url_title($this->input->post('article_title'), '-', TRUE),
'article_category' => $this->input->post('article_category'),
'is_popup' => $this->input->post('is_popup'),
'article_content' => $this->input->post('article_content'),
'article_date' => $this->input->post('article_date'),
);
if ($image)
{
$insert_data['article_image'] = $image['file_name'];
}
$article_id = $id;
$this->db->where('id', $id);
$this->db->update('article', $insert_data);
$this->db->where('article_id', $id);
$this->db->delete('article_tag');
foreach ($this->input->post('article_tag') as $tag)
{
// Check apakah tag itu udah ada di database?
if (is_numeric($tag))
{
$tag_id = $tag;
}
else
{
$this->db->insert('tag', array('tag_name' => strtolower(trim($tag)), 'tag_slug' => url_title(trim($tag), '-', TRUE)));
$tag_id = $this->db->insert_id();
}
if ( ! $this->db->get_where('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id))->row_array())
{
$this->db->insert('article_tag', array('article_id' => $article_id, 'tag_id' => $tag_id));
}
}
}
}
What should i do guys? i did backup but it remain same like after upgraded. Thank you. Web Url (http://www.hidupseimbangku.com/)
First. If you want to upgrade CI Version you must follow Upgrading Guide one by one. There may be break changes and you need to correct them, you can find this guide here:
https://www.codeigniter.com/userguide3/installation/upgrading.html
I suggest you rollback upgrade changes and start again doing one by one. It is not hard, it is just time consuming.
The error you're getting is probably related to an error of uploading process. What is file_upload?
I also suggest you read this, for properly file upload.
I have a table called chat which is joined with users and messages (messages are joined with users), the query works fine just I am trying to add orderby with created_at (desc) but it has to be from the table messages. help me out here. Thanks in advance. Below is my query,
return new GroupDirectChat(
Chat::with(['messages.users', 'users' => function ($query) {
$query->where('id', '!=', Auth::id());
}])
->where('direct_chat', 1)
->whereHas('users', function ($query) {
$query->where('id', '=', Auth::id());
})
->get()
);
Combine a JOIN with a subquery:
$chats = Chat::select('chats.*')
->join('messages', 'messages.chat_id', 'chats.id')
->where('chats.direct_chat', 1)
->where('messages.id', function ($query) {
$query->select('id')
->from('messages')
->whereColumn('chat_id', 'chats.id')
->latest()
->limit(1);
})
->whereHas('users', function ($query) {
$query->where('id', '=', Auth::id());
})
->latest('messages.created_at')
->with(['messages.users', 'users' => function ($query) {
$query->where('id', '!=', Auth::id());
}])
->get();
Note that this query only selects chats with at least one message.
Here is the query I am using to get results of a camp:
$camp = Camp::where('camps.id', $camp_id)
->with(['athletes' => function ($q) use ($camp_id) {
$q->with(['kickoffs' => function ($q) use ($camp_id) {
$q->where('camp_id', $camp_id);
$q->orderBy('id', 'desc');
}]);
$q->with(['kickoff_results' => function ($q) use ($camp_id) {
$q->where('camp_id', $camp_id);
$q->orderBy('score', 'desc');
}]);
But the results are not getting ordered correctly. I have learned that I must join the tables so now my query looks like this:
$camp = Camp::where('camps.id', $camp_id)
->with(['athletes' => function ($q) use ($camp_id) {
$q->with(['kickoffs' => function ($q) use ($camp_id) {
$q->where('camp_id', $camp_id);
$q->orderBy('id', 'desc');
}])->join('kickoff_results', 'athletes.id', '=', 'kickoff_results.athlete_id')
->orderBy('kickoff_results.score', 'desc');
But this seems to be returning me the same thing. I feel like my results are more accurate with my first query, but the ordering is incorrect.
Any suggestions are greatly appreciated!
Create foreign key in the secondary table
and join the table as follows:
DB::table('leads')->select('sec_tbname.sec_tbfk','sec_tbname.join_pm','prm_tbname.prm_tbpm')
->join('pm_tbname','sec_tbname.sec_tbfk','=','pm_tbname.id')
->get();
Hy,
I have some problem vith Zend_Db_Select.
I have 2 variable : category and city. These 2 variables may have a value or they may be unset.
So I verify:
$status = '`p`.status = 1';
if($catID){
$catSQL = "`p`.parent = {$catID}";
}else{
$catSQL = '1=1';
}
if($city){
$citySQL = "`pm`.`meta_key` = 'oras' and `pm`.`meta_value` = {$city}";
$citySelect = array('pm' => 'postsmeta');
$condCity = "`p`.`ID` = `pm`.`parent_id`";
}else{
$citySQL = '1=1';
$citySelect = NULL;
$condCity = '1=1';
}
Now here is my query:
$select = $db->select()
->from( array('p' => 'posts'))
->from($citySelect)
->where($status)
->where($catSQL)
->where($condCity)
->where($citySQL)
;
The problem is that if city is empty I have something like
$select = $db->select()
->from( array('p' => 'posts'))
->from('')
->where(1=1)
->where(1=1)
->where(1=1)
->where(1=1)
;
The questions is how can I remove from('') from my query if city is empty.
Thank you!
Simply,
$select = $db->select()
->from( array('p' => 'posts'));
if($someConditionIsTrue) {
$select->join($table, $condition);
}
$select->where('field_value = ?', 'value1');
if($someConditionIsTrue) {
$select->where('another_field = ?', 'value 2');
}
Hope it helps.
Please use this syntax $select->where('another_field = ?', 'value 2'); for proper escaping values to prevent SQL injections.