I have an images table with a column called type. I simply want to update all the rows to change the type to gallery where the user_id matches a particular user.
I am using this code
$this->Image->updateAll(array('Image.type' => 'gallery'),
array('Image.user_id' => $this->Auth->user('id')));
But I get this error: SQL Error: 1054: Unknown column 'gallery' in 'field list'
Why is gallery being added to the field list ?
Isn't the syntax supposed to set type to gallery?
Thanks!
Found this on the manual:
The $fields array accepts SQL expressions. Literal values should be quoted manually.
Thus, the following should work:
$this->Image->updateAll(
array('Image.type' => "'gallery'"),
array('Image.user_id' => $this->Auth->user('id'))
);
In your model do something like this in your method ....
public function saveImage($type='')
{
// I would add a test for $type
$db = $this->getDataSource();
$fields = array('type' => $db->value($type, 'string')); // $db->value() will format strings needed for updateAll()
$condition = array('user_id' => $this->Auth->user('id'));
// I would add a test for user id before running updateAll()
$this->updateAll($fields, $conditions);
}
Related
im use code php in method get to post in column 'tr_no_hp'
like
$nomorhp = $this->input->get('nomor_hp');
but after i try to insert in database column cant be null.
how to fix this problems
this my controllers code
$nomorhp = $this->input->get('nomor_hp');
$data = array(
'us_id' => $this->user->us_id,
'sv_id' => $voucher->sv_id,
'op_id' => $voucher->op_id,
'tr_id_plgn' => $nomor,
'tr_no_hp' => $nomorhp,
);
$this->db->insert('transaksi', $data);
$trx_id =$this->db->insert_id();
and i got this eror code
Query error: Column 'tr_no_hp' cannot be null - Invalid query: INSERT INTO `transaksi`
thanks
Assign default value of 'tr_no_hp' column to NULL in phpmyadmin
You can either modify you tables to allow null (but this might break other things) or set a default value.
For example:
$nomorhp = $this->input->get('nomor_hp') ?: ''; // Default to empty string
Has anyone encountered a bug with table names using the PostgreSQL adapter in ZF2?
I have a column named UserId and when I try to update/delete a row based on this I get this error:
ERROR: column 'userid' does not exist
as opposed to UserId.
This is my code:
$delete = $this->delete(
'Users'
, array(
'UserId = ?' => $UserId
)
);
Might have something to do with autoQuoteIdentifiers but I couldn't find much online about it.
Any ideas?
The problem was that camel cased column names need to be enclosed in double quotes so the array should be:
array('"UserId" = ?' => $UserId)
I have a problem in writing a query with Zend 1.11.3. I'm not able to get a field whose name contains a dot. Since I cannot edit the table I tried escaping the name, with no success.
My code is the following:
<?php
class RoomsTable extends Zend_Db_Table_Abstract {
...
function getRecord($id)
{
$fields = array(
'id' => 'id',
...
'attrib.floor' => 'attrib.floor',
...
);
$Select = $this->select()
->from($this->_name, $fields)
->where('id = ?',$id);
$list = $Select->query()->fetchAll();
}
}
?>
When I call:
$rooms_table = new RoomsTable();
$first_room = $rooms_table->getRecord(1);
I get:
Zend_Db_Table_Select_Exception: Select query cannot join with another table
So, the attrib.floor field is interpreted as an attempt to get a field from a different table, while it is an actual field name from this table.
I tried with the following without success:
'attrib\.floor' => 'attrib\.floor',
'`attrib.floor`' => '`attrib.floor`',
'"attrib.floor"' => '"attrib.floor"',
Do somebody know how to escape the dot, so that Zend allows me to get that field?
http://framework.zend.com/issues/browse/ZF-953 in the comments:
Identifiers that contain a dot (".") character are automatically split on the dot(s), and each piece quoted separately. Thus identifiers support the "schema"."table" or "table"."column" syntax.
If you have a database design that has table names or column names containing a dot character, this should be a very uncommon case, and it is not recommended. But you can work around it by using Zend_Db_Expr and quote the identifier yourself.
http://framework.zend.com/apidoc/1.9/Zend_Db/Expr/Zend_Db_Expr.html
So in this case the right way to write the field would be:
'attrib.floor' => new Zend_Db_Expr('`attrib.floor`')
With CI, I want to insert one record in User table and one in Post table. Below is a brief of my code (two tables will have multiple columns, and I just use one as example).
$this->username=$user;
$this->db->insert('User',$this);
$this->title='my first post';
$this->db->insert('Post', $this);
However, the second insert will be something like "insert into Post (user, title) values ('$user', 'my first post'). And an error is reported that unknown column user in Post.
How can I clear the members in $this before inserting the next records (in another table)?
This happening becouse of
$this->username=$user;
You probably need to use
$this->db->insert('Post', $this->title);
And before you insert, set in title anything you want, but not
$this->db->insert('Post', $this);
However if you still want to work with an object, more information how to do this properly you can find here, http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert
CI used array as the second argument in the insert method. The index will be the column name and the value referred by the index will be the value to be inserted. What you did was you just keep adding into the $this array.
When you first add username the array will look like this(neglecting the db) inside $this :
array( 'username' => $user );
When you add the title, it will become like this:
array( 'username' => $user, 'title' => 'my first post');
See how the previous entry still in there.
You can just unset($this->username);
or you can use another variable to hold your data instead of $this. Example:
$data = array('username' => $user);
$this->db->insert('User',$data);
$data = array('title' => 'my first post');
$this->db->insert('Post',$data);
And you can insert into two columns like this:
$data = array('username' => $user, 'description' => 'i am sleepy');
$this->db->insert('User',$data);
Hope my answer can help you.
why not insert query is working whenever i am using right syn.,i have used this type syntax in my other function of same controller.
code
$reviewData = $this->input->post('reviewData');
$id=1;
$rdaraaa = array(
'id' => $id,
'content' => $reviewData
);
$this->db->insert('reviews', $rdataaa);
please help me
Your variable name is wrong.It should be
$this->db->insert('reviews', $rdaraaa);
You have given wrong array $rdataaa instead of $rdaraaa.And makesure that id,content are the column names in your table reviews.
remove this code print_r($rdaraaa); die; and it should be
$this->db->insert('reviews',$rdaraaa)