$this->Attrriskcontrol->deleteAll(
array(
'Attrriskcontrol.assessid'=>'$assess_id',
'Attrriskcontrol.controlid'=>'$control_id'
));
I want to delete where the value is assessid and controlid in the database. How should i put the values in $assess_id and $control_id?
And is this the right query to delete?
Related
In CakePHP I have two models: Clients & Tickets. A client can have many tickets and a ticket can only have 1 client.
When adding a new ticket I want to automatically create a new client by only entering a name. So the form would be:
Name = "Name client" >> Name should be save in Client table en new client_ID in Ticket table
Info = Ticket information >> Save in Ticket table.
"Save"
I'm not sure how this works. I have associations in the model and tried to saveAll but there is no data stored in the Client table. And how do I get the ID in the Ticket table?
Hope someone can point me in the right direction. I've searched for other answers but cant seem to find a solution. Is saveAll the right way to do this?
You should set a php condition before insert values into the table, the sql request 4 exemple return the numbers of repetition of the same ticket_id so if the returned value is greater than 1 the request can't execute else the request insert the values
The function to count number of rows : mysql_num_rows('request');
You can use the callback methods.
If you have defined your relation in the Ticket model with belongsTo Client, it will be accessible from that model.
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id',
),
);
So, if you want to make a new client before saving the ticket, you can do:
public function beforeSave ($options = array()) {
$this->data['Client']['name'] = $someVar;
$this->Client->save($this->data);
$this->data[$this->alias]['client_id'] = $this->Client->getInsertID();
return true;
}
I have a table of files and a table of permissions. The idea is that the user can assign permissions to other users to allow them to read, edit, etc. I set up an association so that the files, which I call workspaces, have many permissions. Here is the declaration in the workspaces model linking the permissions model
public $hasMany = array(
'Permission' => array(
'className' => 'Permission',
'foreignKey'=> 'workspace_id',
'dependent' => true
)
);
The problem is that when I go to delete a file, I get an error.
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Permission.id' in 'field list'
SQL Query: SELECT `Permission`.`id` FROM `cadwolfc_workspaces`.`permissions` AS `Permission` WHERE `Permission`.`workspace_id` = 71
Why is cakephp looking for an id parameter in the permissions table when I have clearly defined the workspace_id as the foreign key? I had assumed that by linking the models and then deleting a workspace, the associated permissions would be deleted. That is what I am trying to do here. I don't understand why a select query is being executed when a delete query is what is needed?
Update: The call to delete the workspace item is done through an ajax call by passing the id of the workspace. I have confirmed that $fileid is the correct number.
$this->Workspace->delete($fileid);
Update 2: When I add a column to the permissions table titled 'id' add make its value the appropriate number, then the delete call works fine and all items are deleted. Why is this?
I'm trying to add a new row in a wordpress table (that i've created), but every times that i add the new row, all the others are deleted and if i look DB table i can see only last inserted row.
I've tried two methods, but i get the same results:
$ins_reply = $wpdb->insert( $table, array('itemid'=> $item_id, 'time' => $time, 'title' => '', 'text' => $content, 'author' => $user, 'deadline' => 0));
OR
$query_insert = "INSERT INTO ".$table." (`itemid` ,`time` ,`title` ,`text` ,`deadline` ,`author`) VALUES ('".$item_id."', '".$time."', '', '".$content."', '0', '".$user."')";
$wpdb->query($query_insert);
What's wrong in my code?
Your query as posted in your question is not the source of your issue.
Perhaps you are working on a plugin where the table creation script is accidentally wiping & re-installing the table during this operation?
I'm sorry, I am a newbie in CakePHP and I am a little bit confused in this subject, let me explain:
I have a relationship between two tables. One of the table is Dose and the other is tank. So, one Tank belongs to a Dose. A Dose has many Tanks. The table schema is:
CREATE TABLE `doses` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`dose` INT(5) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
In my Tank view I have the following code:
<?php echo $form->input('dose_id', array('class'=>'input', 'label' => ''));?>
Each 'dose' (field) from Dose table correspond to a value, such as 200, 300, and so forth. I need to use these numbers to calculate others numbers before to insert into my database (table tank). For instance, my code in tanks_controllers:
$t_u = $this->data['Tank']['tipo_uso_id'];
if( $t_u == '1'){
$this->data['Tank']['producao_adubo_diaria'] = $this->data['Tank']['dose_id'] * 0.10;
.
.
.
However, it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)? I tried to set up this way in my model:
'Dose' => array(
'className' => 'Dose',
'foreignKey' => 'dose_id',
'conditions' => '',
'fields' => 'dose',
'order' => ''
)
It did not work.
I appreciate your time helping me.
Thanks in advance.
it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)?
You need to get it from the db (model), not from the view. So you need to do a find(). If you are new to Cake, you should read the cookbook first to see how it works.
What does $form->input('dose_id') produce? A dropdown? If so; by default cake will produce a dropdown with the value containing (dose_)id, and the text you see as the value of $displayField(usually name/title).
To do this; if I understand you, you would need to first query doses for all the values and store the result in an array using the dose value as the key AND the value, rather than the id as you normally would. You would then be able to access the actual dose value from $this->data.
$doseArray=array();
$doses = $this->Dose->find('all');
foreach($doses['Dose'] as $k => $v) {
$doseArray[$v] = $v;
}
perhaps. Seems a bit redundant so I might be off.
I have this in my table model called Table
$test = $this->find('first', array(
'conditions' => array('table.test_id is NULL'),
'order'=> array('table.created ASC'),
)
);
it doesnt work. Tryingt to get the latest row with some criteria
Well, first of all, to get the latest row, you would want to organize by the created field descending, rather than ascending. Also, there are some problems with your syntax, that I have cleaned up below.
$this->find('first', array('conditions'=>array('Table.test_id'=>NULL), 'order'=>array('Table.created'=>'desc')));