I have a table, myTable, with a single column, myColumn, with a single row.
I want to change the value of myColumn in that first (and only) row.
I tried this, but nothing happens:
$myNewValue = 'foo';
$this->db->update('myColumn', $myNewValue);
What am I doing wrong?
Obviously it must have something to do with not specifying which table, but I don't know how to do that.
Codeigniter's update() needs to follow this syntax:
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]])
Parameters:
$table (string) – Table name
$set (array) – An associative array of field/value pairs
$where (string) – The WHERE clause
$limit (int) – The LIMIT clause
so besides of adding the correct table name, you'd need to send update data as an array, using this approach:
$myNewValue = array('myColumn'=>'foo');
$this->db->update('myTable', $myNewValue);
Figured out how to do it:
$this->db->update('myTable', [
'myColumn' => $myNewValue,
]);
Try this method, it is my favorite format.
$where = array(
'column_id'=$id
);
$data = array(
'column_name'=$data
);
$this->db->update('table_name', $data,$where);
Related
i want to get data using where in condition
my code is below
SELECT `cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`
FROM (`cuc_UserCardsDetail`)
JOIN `cuc_CreditCardType` ON `cuc_CreditCardType`.`intGlCode` =
`cuc_UserCardsDetail`.`fk_CardTypeGlCode`
WHERE `cuc_UserCardsDetail`.`intGlCode` IN ('10,29')
AND `chrAccountType` = 'M'
ORDER BY `dtCreateDate` desc
now I have resulted with one row actually in my table there is two-row with related id
i think problem is In('10,29') but i want like ('10','29') or (10,29) how can i do in codeigitor?
i want (10,29) or ('10','29') in where in ..
In CI query builder use
$id= array('10', '20'); # or direct assing your input params
$this->db->where_in('column_name', $id);
Read Looking for Specific Data - Codeigniter.com
Method chaining will accept array in where_in
$intGlCode = array(10, 29);
$this->db->select('`cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`');
$this->db->from('cuc_UserCardsDetail');
$this->db->join('cuc_CreditCardType','cuc_CreditCardType.intGlCode =
cuc_UserCardsDetail.fk_CardTypeGlCode');
$this->db->where('chrAccountType','M');
$this->db->where_in('cuc_UserCardsDetail.intGlCode',$intGlCode);
$this->db->order_by('dtCreateDate','DESC');
$data = $this->db->get()->result_array();
Your query look something like
you have to define column chrAccountType from which table, here i assume it from cuc_UserCardsDetail
$this->db->select('ud.*, cc.varValidName');
$this->db->where_in('ud.intGlCode', ['10','29']);
$this->db->where('ud.chrAccountType', 'M');
$this->db->order_by('dtCreateDate', 'desc');
$this->db->join('cuc_CreditCardType cc', 'cc.intGlCode = ud.fk_CardTypeGlCode');
$data = $this->db->get('cuc_UserCardsDetail ud')->result_array();
print_r($data);
where_in() function just in the case of array of data.
$ids = array();
$this->db->where_in('id', $ids);
So I trying to build a notification system (CodeIgniter) and not store it in my database by this unique ID. Now I got some problem to using `SELECT query.
Also this is array stored in "value" row:
[{"id":0,"user_id":"1","comment":"That's a Nice Video.","posttime":1403523177,"status":1},{"id":1,"user_id":"4","comment":"Nice to see this..!!","posttime":1403590409,"status":1}]
And this is my (not work) query:
$query = $this->db->get_where('post_meta',array('status'=>1),in_array('user_id', $id));
Ideas?
Note: Notification will be sparated by "user_id".
You should not use in_array('user_id', $id) on that function because it returns boolean.
On the active record page: https://ellislab.com/codeigniter/user-guide/database/active_record.html you can take a look at parameter it takes for get_where() function
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
Notice how the third parameter takes $limit which talks about the number of data you'll receive. (Leaving this blank will return you all data).
Some code examples:
If you just want to get the data with status = 1, use the following:
$query = $this->db->get_where('post_meta',array('status'=>1));
If you want to get the data with status = 1 and user_id = $id, use the following:
$this->db->like('value', '"user_id":"'.$id.'"');
$query = $this->db->get_where('post_meta',array('status'=>1));
The solution above is not the best, but it should work.
The $this->db->like() function will create rules to get data in value row which has "user_id":"$id" where $id is the value that you define.
What if I want to get all notifications and group them based on their ID?
I usually get all the data and then use PHP to group them into its own array. I think it's cheaper than relying on database to do that (Correct me if i'm wrong).
So use the first code:
$query = $this->db->get_where('post_meta',array('status'=>1));
Then iterate them using foreach() or whatever you find convenient.
$data = array();
foreach($query as $k=>$v) {
// Run a function to get User ID
$user_id = your_function_here($v->value);
if(!isset($data[$user_id]))
$data[$user_id] = array();
// This is just some example to group them together, you can optimize it to your own liking
array_push($data[$user_id],$v);
}
your_function_here() is your function to get the user_id from value row on your database.
I have the problems using the array input as a value in WHERE clause.
But don't want to use more than once in WHERE clause code.
In my case, this is what I want :
$cond = array('job_id' => $job_id_var, 'job_name' => $job_name_var);
//WHERE clause
$this->where($cond); //only using once WHERE clause code like this, array as input
//which means
WHERE job_id = '$job_id_var' AND job_name = '$job_name_var'
is it possible to do that in codeigniter?
Yes, the ->where() method can support that.
Since you do not want to cascade it:
$this->db->where('job_id', $job_id_var);
$this->db->where('job_name', $job_name_var);
->where() can handle array input as well:
$cond = array('job_id'=>$job_id_var, 'job_name'=>$job_name_var);
$this->db->where($cond); // here, only used once.
$query = $this->db->get('hello_table');
$result = $query->result_array();
return $result;
I have in my table "Artiste" one column "valideAdmin" who takes value 1 or 0.
I try to make a simple count to return the number of entries in my table where "valideAdmin" is to 1:
$repo = $this ->getDoctrine()
->getManager()
->getRepository('ProjectMainBundle:Artiste');
$qb = $repo->createQueryBuilder('valideAdmin');
$qb->select('COUNT(valideAdmin)');
$qb->where('valideAdmin=1');
$count = $qb->getQuery()->getSingleScalarResult();
return array(
'count' => $count
);
But it always "1" who's return...
Without where clause, I have the total count of the entries of the table, but valideAdmin can be 0 or 1. I only want the count number where valideAdmin=1
Thanks for help
createQueryBuilder()'s first parameter is the alias that you want your entity to take (ie.: a short name to be used to refer to your entity in the query).
What you need to do is set a proper alias for your entity (for example a for Artiste) and then COUNT() the instances of your entity where the property (not the column) valideAdmin is set to one:
$repo = $this ->getDoctrine()
->getManager()
->getRepository('ProjectMainBundle:Artiste');
$qb = $repo->createQueryBuilder('a');
$qb->select('COUNT(a)');
$qb->where('a.valideAdmin = :valideAdmin');
$qb->setParameter('valideAdmin', 1);
$count = $qb->getQuery()->getSingleScalarResult();
Remember that DQL runs queries on entities. The DQL your write is then translated into SQL to query the underlying data source after.
Also you can fetch all date then use of COUNT function in PHP
This method has an advantage.You do not have to use a complex query.
You have all the information with count columns
$repositoryArtiste = $this->getDoctrine()
->getRepository('ProjectMainBundle:Artiste');
$queryArtiste= $repositoryArtiste->createQueryBuilder('a')
->Where('a.valideAdmin = :valideAdmin')
->setParameter('valideAdmin',1)
->getQuery();
$Artiste = $queryArtiste->getResult();
var_dump(count($Artiste));
How to count the total number of rows of table and pass it to the variable? For example, I have product table and there are 10 products in it. I want to count all the rows and get the result 10 and pass it to the variable $rowcount. How can I do it?
use find('count')
For example:
$total = $this->Article->find('count');
If you want to use a condition in your query, then pass the conditions array, here is a sample code:
$pending = $this->Article->find('count', array('conditions' => array('Article.status' => 'pending')));
you also try this with condition
$rowcount = $this->Product->find('count', array('conditions' => array('Product.fieldName' => 'value')
u can do it with find('count') method
$this->set('rowcount',$this->Product->find('count'));
or simply use count() function if had already the products in a variable $products to avoid another Mysql query
$this->set('rowcount',count($products));
By Using this you can count data with passing conditions in where
$assignment = TableRegistry::get('Assignment');
$query = $assignment->find();
$query->select(['count' => $query->func()->count('id')]);
$query->where(['Assignment.user_id'=>$user_id]);
$assignment = $query->toArray();
$assignment_count = $assignment[0]->count;
return $assignment_count;
In CakePHP 3.x you can query like this:
$count = $this->Students->find()->count();
Or
TableRegistry::get('Students')->find()->count();
Here student is example model. Replace with your model name.
Thanks.