i am working on a Cakephp 2.x ...i want to delete a single record from the database
i have a table in my database called Image .. which contain following fields..
idImage,User_id,filename,filesize,filemime
I would like to delete the single record with idImage=imageid
delete * from posts where imageid = 3'
i am doing this ..image id now has a value 4
$this->Image->imageId = $imageId;
if($this->Image->delete()){
echo "successfull";
}else{
echo "not";
}
but the code is not working .not deleting the record from db
id, not imageId
Setting imageId on the Image model will have no effect at all. If the model's primary key is not id it's necessary to configure the model so that Cake is aware of this and will use your chosen primary key field instead of id.
<?php
class Image extends AppModel {
$primaryKey = 'imageId';
Irrespective of the value of the primary key, the property to set to use the syntax in the question is:
$this->Image->id = $imageId;
if($this->Image->delete()){
This will attempt to delete the image with the given $imageId.
Alternatively just pass it to the delete function:
if($this->Image->delete($imageId)){
Deleting without a primary key
If the objective is to delete by a value that isn't the primary key, either look for it first:
$id = $this->Image->field('id', array('imageId' => $imageId));
if ($id && $this->Image->delete($id)) {
...
Or use deleteAll:
// delete 1 or more records with a matching field value
$this->Image->deleteAll(array(
'imageId' => $imageId
));
Related
I'm try'n to delete a record using JTable delete() on a multi-column table. Table is a linked table defined as:
CREATE TABLE IF NOT EXISTS `#__bb_league_members` (
`user_id` int(10) unsigned NOT NULL ,
`league_id` int(10) unsigned NOT NULL ,
`status` TINYINT NULL DEFAULT 0,
....
PRIMARY KEY (`league_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
since I have no single primary key, I can not use .delete($pk) but I have to load the record into a JTable instance.
$data = [];
$data['user_id'] = $uid;
$data['league_id'] = $lid;
$tbl = $this->getTable('LeagueMember');
$tbl->load($data); //a var dump here shows the record is loaded!!
return $tbl->delete();
according to the Joomla 3.6 docu on JTable.delete($pk)
$pk is: "An optional primary key value to delete. If not set the instance property value is used."
So if I omit the $pk the current loaded instance should be deleted. However I get a "Null primary key not allowed." exception from my code above.
How do I delete a record with a multiple-column key?
PS: I know I can use a SQL statement directly, but my table classes are set up to do tracing/logging, and I would like to stick with using them.
After a little bit more digging I found a 2009 forum post here which states that JTable does not support multi-column or compound keys. :(
So for now, I had to extend JTable with my own multi_column_key_delete() and ... update() functions
The entire error message is below:
Unable to update the EntitySet '' because it has a DefiningQuery and no element exists in the element to support the current operation.
So everything I've read is talking about not having a Primary Key. However I have a primary key. I have a composite primary key of a couple fields. Is it not possible to delete a record with entity framework with a composite primary key on the table? If this is the case it seems silly to add 1 field just for this purpose. What are my other options?
Here is what I'm doing (an example)
var recs = (from a in dbContext.mytable where a.State == 'Texas' select a);
foreach(var d in recs)
{
dbContext.myTable.Remove(d);
}
dbContext.SaveChanges(); // ERROR
Imagine that mytable has 10 fields and 4 or them make up the PK.
In my Project I have items table this contain two foreign Keys locations_id and categories_id. When i add new item, cateogries_id appear categories.name
in add.ctp : select options (foreign) Key is display Id.
I want to appear at location select option locations.city field from location table.
this is used by cake bake command to my all tables.!
Thanks
\src\Model\Table\LocationsTable.php
Change Display Field. this is show Id ..
now Change
$this->displayField('id'); To $this->displayField('name'); /*name is your field in you table you want to display
I am working on a cakephp 2.x , I want to create a new row or record if the userid is not present in db otherwise update the record, but the problem is: it is not updating the current record. It is creating the record with the userid "0" I dont know the problem is. If is not finding the record it should create a record with the userid which I am giving.
public function activeNoStatus(){
$this->loadModel('Status');
$userid = $this->request->data('idUser');
$notify = $this->request->data('notify');
$data = array();
$data['activeNo'] = $notify;
$count = $this->Status->findUserId($userid);
if($count>0){
$this->Status->id = $userid;
$this->Status->save($data);
echo "update";
}else{
//create new row
$datanew=array();
$this->Status->id = $userid;
$this->request->data['Status']['activeNo']=$notify;
$this->Status->save($this->request->data);
echo "ok";
}
}
It seems idUser field is not autoincrement, In cakePHP documentaion :
All tables with which CakePHP models interact (with the exception of
join tables), require a singular primary key to uniquely identify each
row. If you wish to model a table which does not have a single-field
primary key, CakePHP’s convention is that a single-field primary key
is added to the table. You have to add a single-field primary key if
you want to use that table’s model.
Rather than using an auto-increment key as the primary key, you may
also use char(36). Cake will then use a unique 36 character uuid
(String::uuid) whenever you save a new record using the Model::save
method.
So you should make idUser autoincrement Primary key.
I want to save some data from an external source that has no id other than the data itself (eg. Red, Used, Volvo etc. from the ebay api).
So I have set up my tables like so:
aspectHeaders
id INT AUTO_INCREMENT PRIMARY KEY
name varchar(30)
UNIQUE KEY `NAME`(`name`)
aspects
id INT AUTO_INCREMENT PRIMARY KEY
name varchar(30)
aspectHeader_id INT
UNIQUE KEY `DETAIL` (`aspectHeader_id`,`name`)
aspectHeaders would contain:
7 Manufacturer
and aspects would contain:
1 Volvo 7
So in 2 stages I could check for the existence of any given data in either table and then insert it if it doesn't exist. But my question is can I do it in 1 stage? That is, is there code to check if the data exists and insert it if not and either way return the id?
Hope this is verbose enough.
Thanks
If by "code" you mean a single MySQL statement, I don't think there is. Or at least I don't think there is without making an overly-complicated multi-query query.
Just make an alreadyExists() method (or something) and check before insert - something like:
if(!$this->MyModel->alreadyExists($data)) {
//do insert here
}
I usually use a method like touch() for this that does all the magic internally.
// always returns id (or false on failure)
public function touch($data) {
// check if it already exists
if (exists) {
// return id and do nothing
}
// create() and save()
// return id or false if invalid data
}