I am trying to add an entry to an Oracle table which has a date field. So far, I've only been able to do it like this:
$createdDate = $entry->createdDate->toString('yyyy-MM-dd');
$data = array(
'ID' => $entry->id,
'STATE' => $entry->state,
'CREATED_DATE' => new Zend_Db_Expr("to_date('$createdDate', 'YYYY-MM-DD')")
);
$this->_getGateway()->insert($data);
Is there a better way? This solution smells dirty to me.
You should be able to do this instead:
$createdDate = $entry->createdDate->toString('yyyy-MM-dd');
$data = array(
'ID' => $entry->id,
'STATE' => $entry->state,
'CREATED_DATE' => new Zend_Db_Expr("date '$createdDate'")
);
$this->_getGateway()->insert($data);
The only difference is the use of an ANSI date literal in line 5.
Related
i've some problem with my code.
$id = DB::table('sn_project_details')->insertGetId([
'emp_name' => $request->emp_name,
'emp_id' => $request->emp_id,
'department' => $request->department,
'submit_date' => $request->submit_date,
'total_amount' => $request->total_amount,
'project_tittle' => $request->project_tittle,
'project_desc' => $request->project_desc,
'scope' => $request->scope,
'file' => $request->file
]);
//Update Table
\DB::table('sn_project_details')
->where('project_id', $id)
->update(['doc_ref' => "ID_",$request->scope,"_",$id]);
return redirect('/user')
I want to update column doc_ref with example value ID_Scope_220,
ID_ its fixed value. Scope from textbox scope. 220 from #emp_id.
but when i execute this code, update query not working properly.
can someone help? thx
use dot instead of comma
->update(['doc_ref' => "ID_".$request->scope."_".$emp_id]);
What to do if I want to add all the file names in single column in mysql database
$data = array(
'name' => $this->input->post('pd_name'),
'image' => $product_image,
'image' => $product_image1,
'image' => $product_image2,
'created_time' => date('Y-m-d H:i:s')
);
this doesn't work
You can implode the variables while storing into a single column.
$image_data = array($product_image, $product_image1, $product_image2);
implode('#',$image_data);
when you want to display just explode them.
explode('#',$image_data);
I don't get the difference between query builder's Replace and Update. Especially the documentation for Replace...
This method executes a REPLACE statement, which is basically the SQL standard for (optional) DELETE + INSERT, using PRIMARY and UNIQUE keys as the determining factor.
...but I see no indication of using a PK in the example. Am I missing some fundamental knowledge here? (I understand Update just fine).
Replace
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->replace('table', $data);
Update
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Thanks.
REPLACE
It's like insert. but if the primary key being inserted is the same one as another one, the old one will be deleted and the new one will be inserted.
UPDATE
Updates the current row that you tried to update.
In CodeIgniter, I want to insert a row into a db that includes a timestamp for "now".
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => "NOW()" // hmmm how do I do this?
);
$this->db->insert('messages', $message_row);
If I use "NOW()" it will just enter the string, and not actual have MySQL use its NOW keyword.
In order to use NOW() function in CodeIgniter you need to use "set" function
$table = 'user';
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email
);
$this->db->set('send_date', 'NOW()', false);
$this->db->insert($table, $message_row);
if you need to be timezone aware, i recommend you store the datetime in UTC format.
so:
$now = new DateTime ( NULL, new DateTimeZone('UTC'));
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => $now->format('Y-m-d H:i:s')
);
when you retrieve the information, you have to know the user's timezone, using a $row that contains the send_date:
$then = new DateTime( $row->send_date, new DateTimeZone('UTC'));
$then->setTimeZone(new DateTimeZone($userstimezone));
echo $then->format('Y-M-D H:i:s');
I can't get the values from a second level contain using different field name as link.
Asistencia model:
var $belongsTo = array('Employee');
Horario model:
var $belongsTo = array('Employee');
Employee model:
var $hasMany = array(
'Horario' => array(
'foreignKey' => 'employee_id',
'dependent' => true
),
'Asistencia' => array(
'foreignKey' => 'employee_id',
'dependent' => true
)
);
I'll explain using these values on my example record:
Asistencia: employee_id = 3701
Employee : id = 3701
In my find() from Asistencia, I get to contain Employee by switching Employee primaryKey just fine:
$this->Asistencia->Employee->primaryKey = 'id';
$this->paginate = array(
'contain' => array(
'Employee' => array(
'foreignKey' => 'employee_id',
//'fields' => array('id', h('emp_ape_pat'), h('emp_ape_mat'), h('name')),
'Horario' => array(
'foreignKey' => 'employee_id',
//'fields' => array('id' )
))
),
'conditions' => $conditions,
'order' => 'Asistencia.employee_id');
However, my Horario record is linked to Employees via another field: emp_appserial
Employee : emp_appserial = 373
Horario : employee_id = 373
How can my Employee array contain Horario array? (they do share the value just mentioned).
Currently, the Horario contain is using the value on Asistencia.employee_id and Employee.id (3701). (checked the sql_dump and is trying to get the Horario via
"WHERE `Horario`.`employee_id` = (3701)"
but for the Employee to contain Horario, it should use the value on Employee.emp_appserial and Horario.employee_id (373).
This is what i get (empty array at bottom)
array(
(int) 0 => array(
'Asistencia' => array(
'id' => '5',
'name' => null,
'employee_id' => '3701',
'as_date' => '2012-11-19',
),
'Employee' => array(
'id' => '3701',
'emp_appserial' => '373',
'emp_appstatus' => '8',
'AgentFullName' => '3701 PEREZ LOMELI JORGE LORENZO',
'FullNameNoId' => 'PEREZ LOMELI JORGE LORENZO',
'Horario' => array()
)))
Please notice:
'employee_id' => '3701', (Asistencia)
and
'emp_appserial' => '373', (Employee)
my Horario has 'employee_id' = 373.
How could I make the switch so the relation Employee<->Horario is based on emp_appserial?
Thank you in advance for any help.
Firstly you may be getting problems because you're using Spanish words for your Model names and I suspect you're also using them for Controllers.
This is a very bad practice since CakePHP's idea is:
Convention over configuration.
This is achieved through the Inflector class which "takes a string and can manipulate it to handle word variations such as pluralizations or camelizing and is normally accessed statically". But this works ONLY WITH English words. What this means for you is that you may be missing the data because Cake is not able to build the DB queries right, since you're using Spanish words. By doing so you're making the use of CakePHP's flexible persistence layer obsolete - you will have to make all the configurations by hand. Also most likely pagination will NOT WORK. Other parts of the framework may also not work properly:
HtmlHelper, FormHelper, Components, etc. ...
These problems will expand if you try to use more complex Model associations such as HABTM or "hasMany through the Join Model".
So I do not know why exactly you aren't seeing the Horario record, but what I just explained most likely is your problem.
What you're trying to do is against the core principles of CakePHP and you'd save yourself a lot of problems if you refactor a bit and use English words. Your other option is NOT TO use Cake.