I am working on CI 2.1.3 and encountering the following problem. What am I doing wrong here? Is there any reading or knowledge I need to do or know.
Codeigniter ActiveReocrd Update syntax
$data['spun'] = TRUE;
$this->db->update('registered', $data,
"registered_id = $registration['registered_id']");
what I expected
UPDATE `registered` SET `spun` = 1 WHERE `registered_id` = 1
what CI generated
UPDATE `registered` SET `spun` = 1 WHERE `id` = 1
DB table (in bracketing notation)
registered(registered_id, registered_name, ..., spun);
Edit 1
I also tried the following, but CI give the same SQL.
$this->db->update('registered', $data,
array('registered_id' => $registration['registered_id']));
I think the solution is very easy. You can simply do this:
$data['spun'] = TRUE;
$this->db->update('registered', $data,
array('registered_id' => $registration['registered_id']);
Based on Code Igniter documentation:
http://codeigniter.com/user_guide/database/active_record.html#update
you should use:
$data['spun'] = TRUE;
$this->db->where('registered_id', $registration['registered_id']);
$this->db->update('registered', $data);
Related
How can I truncate a table with CakePHP 3.x
I get the truncate query by this:
$this->Coupons->schema()->truncateSql($this->Coupons->connection());
but what is the best practice to execute it
This code working well, thanks to #ndm for his comment that helped the answer to be better.
//In Coupons Controller
$this->Coupons->connection()->transactional(function ($conn) {
$sqls = $this->Coupons->schema()->truncateSql($this->Coupons->connection());
foreach ($sqls as $sql) {
$this->Coupons->connection()->execute($sql)->execute();
}
});
In CakePHP4 you can use the following code to truncate a table:
$table = $this->Coupons;
$sqls = $table->getSchema()->truncateSql($table->getConnection());
foreach ($sqls as $sql) {
$table->getConnection()->execute($sql)->execute();
}
I tested following and it's worked:
$connection = $this->Coupons->getConnection();
$connection->query('TRUNCATE coupons');
Reference: https://book.cakephp.org/4/en/orm/database-basics.html#executing-queries
Read more here: https://book.cakephp.org/4/en/orm/database-basics.html#using-transactions
I tried updating a column value using sql command, but it show general failure. Below is my code for update:
$name = 'ABC';
$id = 2;
$command = Yii::$app->db->createCommand()
->update('companies', ['company_name' => $name], 'company_id ='.$id.'');
$result = $command->queryAll();
When I execute this code below message is shown to me.
SQLSTATE[HY000]: General error
The SQL being executed was: UPDATE companies SET company_name='ABC' WHERE company_id =2
Error Info: Array
(
[0] => HY000
)
I cant find out why. Does anybody have any idea, what am I doing wrong here?
UPD
$command = Yii::$app->db->createCommand()
->update('companies', ['company_name' => $name], 'company_id ='.$id.'')->execute();
Can not use $command->queryAll() with update command.
There are couple of errors in your code.
First of all, why you are using queryAll() with UPDATE operation? Remove this line:
$result = $command->queryAll();
Second error - missing call of execute() command. Should be:
$command = Yii::$app->db
->createCommand()
->update('companies', ['company_name' => $name], 'company_id ='.$id.'')
->execute();
Check out documentation for yii\db\Command, especially execute() and queryAll() methods.
I am trying to get the ID of a last row I've inserted with a php/mysql query:
$createjob = $modx->query($createjob);
$lastId = $modx->lastInsertId();
but this does not seem to be working.
Does any one know the correct way of doing this with ModX PDO?
Try this:
$createJob = $modx->newObject('CreateJob');
$createJob->set( 'value', 1234 );
// try saving
if( $createJob->save() ){
echo $modx->lastInsertId();
}
Read more here
Try simple sql query which gives the max id for record with specified class.
$q = $modx->newQuery('modResource');
$q->select(array(
"max(id)",
));
$id = $modx->getValue($q->prepare());
I need the following condition (in SQL) to fill a specific field in my resultset:
CASE
WHEN M.ID_ENTIDAD = m.ID_ENTIDAD_VENTA then EC.CLAVE_ENTIDAD
END AS Contraparte }
If I use
var contraparte = Projections.Conditional(
Restrictions.EqProperty("EntidadOwner", "EntidadVenta"),
Projections.Property("enc.CvePrincipalMiembro"),
null);
That return an error.
Also if I use:
*var contraparte = Projections.Conditional(
Restrictions.EqProperty("EntidadOwner", "EntidadVenta"),
Projections.Property("enc.CvePrincipalMiembro"),
Projection.Constant(a constant value);*
Apparently it is not possible to use this Conditional without ELSE part. that is nhibernate can not generate CASE without ELSE part.
It is possible to do this?? Please help me!!!
Thanks
Ok. I found self the response.
I took two actions:
One, add the following dummy field in the map class used in your query:
Map(x => x.Dummy).Nullable().Formula("NULL");
Second: I modified the conditional projection with the following code:
var contraparte = Projections.Conditional(
Restrictions.EqProperty("EntidadOwner", "EntidadVenta"),
Projections.Property("enc.CvePrincipalMiembro"),
Projections.Property("Dummy"));
That was all! I hope that this help another people with the same problem.
I'm having an issue where I'm building an ORM query based on several conditions from a $_POST. The final query looks fine and returns records in a direct SQL query (phpmyadmin) but in my application does not return any records. here is the code...
$records = ORM::factory('record')->where(array('date >='=>$_POST['fromdate'],'date <='=>$_POST['todate']));
if ($_POST['agent'] != '0') $records->where(array('ccp_id'=>$_POST['agent']));
if ($_POST['supervisor'] != '0') {
$ccps = ORM::factory('employee')->where(array('supervisor_id'=>$_POST['supervisor'],'active'=>'1'))->find_all();
foreach ($ccps as $ccp) {
$agents[] = $ccp->id;
}
// echo kohana::debug($agents);
$records->in('ccp_id',$agents);
}
if ($_POST['lead'] != '0') $records->where(array('lead_id'=>$_POST['lead']));
if ($_POST['reasons'] != '[]') {
$reasons = explode(',',str_replace(array('[',']','"'),'',$_POST['reasons']));
$records->in('reason_id',$reasons);
}
$records->find_all();
$records->loaded is false. If I change out the find_all() with count_all() I get an accurate count.
With sample data in the $_POST I have this query in $records->last_query()
SELECT `records`.*
FROM (`records`)
WHERE `date` >= '2010-10-10'
AND `date` <= '2010-11-09'
AND `ccp_id` IN ('E128092','E128093','E124874','E124414','E129056','E137678','E078952','E112701','E084457','E098047','E099221','E001131','E120892')
AND `lead_id` = 'E110873'
AND `reason_id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
ORDER BY `records`.`id` ASC
this returns 4 records in phpmyadmin and (4) for count_all().
I do not understand why this is happening. Any insights would be helpful. Thank you.
In your last line you should have
$records = $records->find_all();
instead of
// this actually returns you the resultset and resets the query builder object
$records->find_all()
$records is a Database_Result and has no loaded property. Use count($records) or iterate it with foreach statement to get ORM objects.
Just a note: It's probably better not to wipe out the ORM object ( $results = $records->find_all() instead of $records = $records->find_all()) if you wish to use $records->count_all() or other calls later in your code. Just an issue I ran into.