I've got a a table with a column request_time. There will be multiple requests and I want to get the latest request of a particular username:
$this->Requests->find('all', 'conditions' => array (
'username' => $username,
'MAX'=> 'request_time'));
The above doesn't work. Is there something in Cakephp or do I need to do my own ->query()?
You can use the following:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('id' => 'DESC') ));
Where id is the auto incremented primary key.
This will give you the latest (single) record, if you are using first in find method, or else use all instead.
If you are not using primary key, you can try the following:
$this->Requests->find('first', array('conditions' => array('username' => $username),
'order' => array('request_time' => 'DESC') ));
If you have auto-incremented primary key then you find the latest record of this table try this
$this->Requests->find('find', 'conditions' => array ('username' => $username),
'order' => array('id' => 'DESC')
);
Related
I want to get related products based on some criteria on product attributes like, the same sku, ean and gtin codes. But my query doesn't return any results. I tried different ways to query the database but all fail. Here is my query.
$sku = $product->get_sku();
$ean = $product->get_attribute( 'EAN' );
$gtin = $product->get_attribute( 'GTIN' );
$query = new WP_Query( array(
'post_type' => array('product'),
'posts_per_page' => 20,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'pa_ean',
'field' => 'value',
'terms' => $ean,
'operator' => '=',
),
array(
'taxonomy' => 'pa_gtin',
'field' => 'value',
'terms' => $gtin,
'operator' => '=',
)
)
) );
By the way, if a product has ean code than it doesn't have a gtin and vice versa. It always has a sku which is unique to it self. But I have it added as a check to check if query at least returns value.
I can't find good documentary on this topic and hope someone here can help.
I have two tables:
1- beoordelingen
2 - sterren
I need to update 3 values in table beoordelingen and insert new values into table sterren
How do I do that?
ON DUPLICATE KEY doesn't work and I can't find anywhere how to do it.
$sql=$dbo->prepare("BEGIN;INSERT INTO beoordelingen(beoordelingid,userid,werkid,inlijn,
sjabloon,conclusie,save)
VALUES ('$beoordelingid','$user','$werkid','$inlijn','$sjabloon','$conclusie','0')
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),userid=VALUES(userid), werkid=VALUES(werkid),
inlijn=VALUES(inlijn),sjabloon=VALUES(sjabloon),conclusie=VALUES(conclusie),
save=VALUES(save);
INSERT INTO sterren (beoordelingid,userid,werkid,titelwerk,actie,sterren)
VALUES ('$beoordelingid','$user','$id','$titelwerk',
'$actie','$sterren'); COMMIT");
I adapted the following. The insert1 still won't work. Insert2 works
$dbo->beginTransaction();
//NOT WORKING
$insert1 = $dbo->prepare("INSERT INTO beoordelingen(beoordelingid, userid, werkid, inlijn, sjabloon, conclusie, save)
VALUES (:beoordelingid, :user, :werkid, :inlijn, :sjabloon, :conclusie, '0')
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),
userid=VALUES(userid),
werkid=VALUES(werkid),
inlijn=VALUES(inlijn),
sjabloon=VALUES(sjabloon),
conclusie=VALUES(conclusie),
save=VALUES(save);"
);
//THIS WORKS
$insert2 = $dbo->prepare("INSERT INTO sterren (beoordelingid, userid, werkid, titelwerk, actie, sterren)
VALUES (:beoordelingid, :user, :id, :titelwerk, :actie, :sterren);"
);
//NOT WORKING
$insert1->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'werkid' => $werkid,
'inlijn' => $inlijn,
'sjabloon' => $sjabloon,
'conclusie' => $conclusie,
'save' => $save
));
//THIS WORKS
$insert2->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'id' => $werkid,
'titelwerk' => $titelwerk,
'actie' => $actie,
'sterren' => $sterren
));
$dbo->commit();
My last problem:
$sterren=$_POST['sterren'];
$user=$_REQUEST['user'];
$userid=$_POST['userid'];
$actie=$_POST['actie'];
$dbo->beginTransaction();
//THIS WORKS
$insert1 = $dbo->prepare("INSERT INTO sterren(sterren,userid,actie)
VALUES (:sterren, :userid, :actie);"
);
//THIS DOESN"T WORK
$insert2 = $dbo->prepare("INSERT INTO sterren(sterren,userid,actie)
VALUES (:-sterren, :user, :actie);"
);
//THIS WORKS
$insert1->execute(array(
'sterren' => $sterren,
'userid' => $userid,
'actie' => $actie
));
//THIS DOESN"T WORK
$insert2->execute(array(
'-sterren' => $sterren,
'user' => $user,
'actie' => $actie
));
$dbo->commit();
This is not how to use prepared statements (->prepare()) or how to use a transaction with PDO.
First off, prepare() is for "preparing" one SQL query with placeholders so that you can pass the parameters in a later function (execute()) and avoid SQL injection from concatenating strings.
Second, if you want to use a transaction then you should use PDO's beginTransaction and commit functions.
Try something like this:
$dbo->beginTransaction();
$insert1 = $dbo->prepare('INSERT INTO beoordelingen(beoordelingid, userid, werkid, inlijn, sjabloon, conclusie, save)
VALUES (:beoordelingid, :user, :werkid, :inlijn, :sjabloon, :conclusie, :save)
ON DUPLICATE KEY UPDATE
beoordelingid=VALUES(beoordelingid),
userid=VALUES(userid),
werkid=VALUES(werkid),
inlijn=VALUES(inlijn),
sjabloon=VALUES(sjabloon),
conclusie=VALUES(conclusie),
save=VALUES(save);'
);
$insert2 = $dbo->prepare('INSERT INTO sterren (beoordelingid, userid, werkid, titelwerk, actie, sterren)
VALUES (:beoordelingid, :user, :id, :titelwerk, :actie, :sterren);'
);
$insert1->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'werkid' => $werkid,
'inlijn' => $inlijn,
'sjabloon' => $sjabloon,
'conclusie' => $conclusie
));
$insert2->execute(array(
'beoordelingid' => $beoordelingid,
'user' => $user,
'id' => $id,
'titelwerk' => $titelwerk,
'actie' => $actie,
'sterren' => $sterren
));
$dbh->commit();
following is my code to fetch record from table
$joins=array(
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'User.id= Notification.UserId'
)
)
);
// fetching all records
$returnArray = $this->find('all', array(
'fields' => array('Notification.id','User.Name'),
'joins' => $joins,
'order' => 'Notification.id DESC',
'limit'=>'100',
));
In above code my limit is not working coz i am using joins. can anybody tell me how to use limit with joins in cakephp.
Is there any other method to add limit? PLease tell me ASAP
Thanks in advance
I'm not familiar with Cake, but try an integer instead of string with your limit.
$returnArray = $this->find('all', array(
'fields' => array('Notification.id','User.Name'),
'joins' => $joins,
'order' => 'Notification.id DESC',
'limit'=> 100,
));
I have a table 'activities' and the columns are grouped by three of its attributes. Now I want to fetch Activity ID that is randomly selected from all the set of groups. The query is as follows:
$act = $this->find('all', array('conditions' => $cond,
'limit' => $limit,
'fields' => array('count(*) as count, Activity.id as id'),
'page' => $page,
'group' => $group,
));
These are the things I tried and none of them worked.
1. Create a self join which should have random records
$this->bindModel(array(
'belongsTo' => array(
'Random' => array(
'className' => 'Activity',
'order' => 'rand()',
'foreignKey' => 'id',
'fields' => 'Random.id'
)
)
));
This failed because the rand() order is appended in the end which simply randomizes all fetched activities.
Added a join option
'joins' => array(
array(
'table' => 'activities',
'alias' => 'Random',
'type' => 'LEFT',
'conditions' => array(
'Activity.id = Random.id'
),
'fields' => 'Random.id',
'order' => 'rand()'
)
This also didn't work as order value is not parsed by Cake
Tried to add condition
'Activity.id >= floor(rand() * (max(Activity.id) - min(Activity.id)) - min(Activity.id))'
Gave a mysql error
Please help me out
Assuming that your query is working:
$act = $this->find('all', array('conditions' => $cond,
'limit' => $limit,
'fields' => array('count(*) as count, Activity.id as id'),
'page' => $page,
'group' => $group,
'order' => 'rand()',
'limit' => '1' // if you only want one random activity ID
));
Ok finally figured it out. We need to hack the way cake php works. In place of table name, I wrote a query to fetch randomly ordered records. Also notice the join type is 'right'. Left join wont produce randomly sorted list
$activities = $this->find('all', array('conditions' => $cond,
'page' => $page,
'limit' => $limit,
'fields' => array('count(*) as count, Random.id as id, max(Activity.modified) as Modified'),
'group' => $group,
'order' => 'Modified desc',
'joins' => array(
array(
'table' => '(select * from activities order by rand())',
'alias' => 'Random',
'type' => 'RIGHT',
'conditions' => array(
'Activity.id = Random.id'
),
)
)
));
my problem is very simple. I have a table named 'articles' and I'd like to get the first three recent articles order DESC. But I want also these articles be displayed if their publication date is older or equal to today. So I've had an other condition in my request. My cakephp code is the following :
$this->set('lastArticles', $this->Article->find('all', array(
'conditions' => array('Article.visible' => true),
'Article.publication_date <= ' => date('Y-m-d'),
'order' => array('Article.creation_date DESC', 'Article.id DESC'),
'limit' => 3
)));
But the problem is even if the articles with a publication date > date('Y-m-d') these ones are displayed too! Does anyone have an idea ? Thanks in advance for your answer.
Looks like your publication_date condition isn't in the conditions array. Your code should look like this:
$this->set('lastArticles', $this->Article->find('all',
array(
'conditions' => array(
'Article.visible' => true,
'Article.publication_date <= ' => date('Y-m-d')
),
'order' => array(
'Article.creation_date DESC',
'Article.id DESC'
),
'limit' => 3
)));
A bit of extra indentation and whitespace is your friend here.
See this
$conditions = array(
'Article.visible' => true,
'Article.publication_date <= ' => date('Y-m-d')
);
$order = array('Article.creation_date DESC', 'Article.id DESC');
$lastArticles = $this->Article->find('all', array(
'conditions' => $conditions,
'order' => $order,
'limit' => 3
);
$this->set('lastArticles', $lastArticles);