i am submitting the location from textbox and putting values in comma seprated form like ngp,akola,kanpur.
Now i want if i have 3 values in location textbox then it insert 3 rows in location table one for ngp,second for akola and third for kanpur.
but it also takes last inserted id of user table and post all values in location table.
my location table have fields id,name,user_id.
in user_id column i need the last inserted id of user table and in name column i need to insert all values in table..
please help me to do this..below is my code.
function add() {
if (!empty($this->request->data)) {
$this->User->set($this->data['User']);
$isValidated = $this->User->validates();
if ($isValidated) {
// Generating UUID
if (!empty($this->request->data['User']['company_name'])) {
$this->request->data['User']['is_business'] = 1;
}
if ($this->User->save($this->request->data, array('validate' => false))) {
$lastId = $this->User->id;
$location = implode(',',$this->request->data['User']['location']);
for(i=1;i<=$location;i++){
$this->Location->save($location);
}
}
}
}
}
$lastId = $this->User->id;
$location = explode(',',$this->request->data['User']['location']);
foreach($location as $value){
$data = array();
$data["name"] = $value;
$data["user_id"] = $lastId;
$this->Location->save($data,false);
$this->Location->id = false;
}
Model::saveAll(array $data = null, array $options = array())
$this->request->data['Location'] = explode(',',$this->request->data['User']['location']);
if ($this->User->saveAll($this->request->data, array('validate' => false))){ // ...}
Related
I wrote a function where I get array of marks that i need to post to my database..
My function stores it in a filed row like:
And I need to pull just one per column individually like:
Here is my api call..
public function generate(Map $seatMap)
{
$layout = $seatMap->getSeatLayout();
$seats = [];
$layoutArray = json_decode($layout, true);
$columns = range('A', 'Z');
foreach($layoutArray as $index => $result)
{
$columnLetter = $columns[$index];
$letters = str_split($result);
$letterIndex = 1;
foreach($letters as $letterIndex => $letter) {
switch($letter) {
case 'e':
$seats[] = $columnLetter . $letterIndex;
$letterIndex++;
}
}
}
foreach($seats as $seat => $result) {
$result = new Seat();
$result->setName(json_encode($seats));
$this->em->persist($result);
$this->em->flush();
}
}
Any suggestions?
I think that problem is in the part where I need to store it to database..
If I understand you correctly, your issue is here:
foreach($seats as $seat => $result) {
$result = new Seat();
$result->setName(json_encode($seats));
$this->em->persist($result);
$this->em->flush();
}
}
You're indeed creating new Seat instance for every seat, but in this line:
$result->setName(json_encode($seats));
you still assign all (encoded) seats to every instance of Seat. What you want is to assign only the seat from current loop iteration, which is represented by $result variable.
So try with:
$result->setName($result);
You do not need json_encode here too.
If your array is like you say it it then try this
foreach($seats as $seat) {
$result = new Seat();
$result->setName($seat);
$this->em->persist($result);
$this->em->flush();
}
I'm new to CakePHP. I have a table to keep record of user's activity by creating a log in it. The table has two columns
+----+------------+-----------+
| id | user_id | comment |
+----+------------+-----------+
I want to pass values from within controller like
$this->ActivityLogs->log($user_id, 'Message sent');
log is a custom function inside ActivityLogs model which will record some more data along with passed data
public function log($user_id = null, $message = null)
{
... record code goes here
return true;
}
But couldn't get how to write insert query inside model.
How can I create custom methods like this and also can anyone suggest me good resource to go through model queries and understanding.
public function log($user_id = null, $message = null){
//I assume here that your table name is 'logs'
$logsTable = \Cake\ORM\TableRegistry::get('Logs', array('table' => 'logs'));
$log = $logsTable->newEntity();
$log->user_id = $user_id;
$log->body = $message ;
if ($logsTable->save($log)) {
return true;
}
return false;
}
This question already has an answer here:
Cakephp add function if id exists edit else create
(1 answer)
Closed 6 years ago.
I want to update the record if the ID is exists in my database. If ID not exists create record. This is my code in my Controller add function
public function add () {
$googleCategory = $this->request->data;
foreach ($googleCategory as $key => $value) {
if(empty($value['category'])){
unset($value);
}
$this->AccountShopMeta->create();
$data['shop_id'] = $value['shop_id'];
$data['name'] = $value['category'];
$data['value'] = $value['url_key'];
$data['tag'] = '';
if($this->AccountShopMeta->save($data)){
$account_shop_meta = $this->AccountShopMeta->read();
$this->set($account_shop_meta);
$this->set('_serialize', array_keys($account_shop_meta));
}
}
}
if i understood what you are asking may be this will help you. Do not use Create. if id exist in given array ,updatation automatically will be done on that ID. Otherwise new row will be created.
public function add () {
$googleCategory = $this->request->data;
foreach ($googleCategory as $key => $value) {
if(empty($value['category'])){
unset($value);
}
if(!empty($value[id])){
$data['id']=$value[id];
}
$data['shop_id'] = $value['shop_id'];
$data['name'] = $value['category'];
$data['value'] = $value['url_key'];
$data['tag'] = '';
if($this->AccountShopMeta->save($data)){
$account_shop_meta = $this->AccountShopMeta->read();
$this->set($account_shop_meta);
$this->set('_serialize', array_keys($account_shop_meta));
}
}
}
I suppose if id not exists it comes as empty, in such case add following conditions within foreach and at least above save action:
foreach ($googleCategory as $key => $value) {
if(isset($value['id']) && !empty($value['id'])){
$this->AccountShopMeta->id = $value['id'];
// your remaining code within loop here
//
}
}
Do not forget to send data for id form your view(form) if exists..
Looping to save each column field picture1pic6:
Please help, i using cake php version 2.0, try to do looping to save each column, but only the first row data save, the data should be save like attached picture1.Hope someone can help, i tried to figure out 2 weeks. V_COUNT i hardcode the number to 23.
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
$this->IPI->setSource($table_name);
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$this->request->data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$this->request->data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$this->request->data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$this->request->data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->create();
$this->IPI->save($this->request->data);
}
}
try this
if($this->IPI->save($this->request->data))
{
$table_name = 'IPI_V';
for($i=1;$i<=$this->request->data['IPI']['V_COUNT'];$i++)
{
$data = array();
if($i<10){$i = '0'. $i;}
if(($this->request->data['IPI']['Quantity'.$i])!=NULL)
{
$data['IPI']['Type_Defect'] = $this->request->data['IPI']['Type_Defect'.$i];
$data['IPI']['CAT'] = $this->request->data['IPI']['CAT'.$i];
$data['IPI']['Defect'] = $this->request->data['IPI']['Defect'.$i];
$data['IPI']['Quantity'] = $this->request->data['IPI']['Quantity'.$i];
$this->IPI->setSource($table_name);
$this->IPI->create();
$this->IPI->save($data);
}
}
I need perform query "LOCK TABLE myTable WRITE"
<?php
$db = Database::instance();
$query = 'LOCK TABLE `myTable` WRITE';
$db->query(Database::WHAT_IS_THE_TYPE_SHOULD_SPECIFY_HERE_?, $query);
In framework Kohana 3.3
file ~/modules/database/classes/Kohana/Database.php
implements folowing types:
const SELECT = 1;
const INSERT = 2;
const UPDATE = 3;
const DELETE = 4;
but none of them fit in my case.
Any idea. Thanks.
Google works wonders. http://forum.kohanaframework.org/discussion/6401/lockunlock/p1
You can pass NULL as the first argument:
$db = Database::instance();
$query = 'LOCK TABLE `myTable` WRITE';
$db->query(NULL, $query);
From Kohana: http://kohanaframework.org/3.3/guide-api/Database_MySQL#query
if ($type === Database::SELECT)
{
// Return an iterator of results
return new Database_MySQL_Result($result, $sql, $as_object, $params);
}
elseif ($type === Database::INSERT)
{
// Return a list of insert id and rows created
return array(
mysql_insert_id($this->_connection),
mysql_affected_rows($this->_connection),
);
}
else
{
// Return the number of rows affected
return mysql_affected_rows($this->_connection);
}
As you can see that's why you're able to pass NULL.