I have a News model that has many Comments. What I need is to find ten News that has new Comments.
At first this task is seems to be eazy, I just need to find last ten Comments ($this->Comment->find('all');) and just display related News, but in case I have 2 comments for the same news I will recieve a duplicated news entry.
So, can I order News by Comments date or something?
*And here is solution. Thanks Dave
$this->Comment->find('all', array(
'order' => array(
'Comment.created' => 'DESC'
),
'group' => 'News.id',
));
Use MySQL's "GROUP BY" ('group' option in CakePHP). More details:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
Related
I'm struggling to make two custom queries for visual composer:
a query which will display posts which are published with a date after today's date (have plugin which publishes future posts) in Ascending or descending order (whichever makes it show the soonest upcoming event first)
a query which will display only posts which meet multiple category requirements (eg. categories 'upcoming' and 'class' or 'upcoming' and 'social'
Really, either of these should give me the results I want, but the first would be most convenient.
I can't for the life of me decipher the Codex's page on custom queries and figure out how to get them to work outside of the context of a php page, and in the context of the visual compose.
One thought I had is that perhaps I need to add a custom query to my functions.php that has a name, and then call the query with the variables from Visual Composer? Is that the right idea? How would I do such a thing/call the new query?
I've tried the following to satisfy #1 and it seems to have disregarded the query altogether and just displayed all the events:
[vc_basic_grid post_type="custom" grid_id="vc_gid:1473741223498-3776c0d3-292b-4" custom_query="WP_Query( ''showposts=20&monthnum=' . date_query( array( 'after', . NOW() . ) ) . '&order_by=date&order=DESC')"]
[vc_separator]
[vc_basic_grid post_type="custom" grid_id="vc_gid:1473741223504-67e7758b-8892-6" custom_query="$args = array(
'date_query' => array(
array(
'after' => NOW(),
'inclusive' => true,
),
),
'posts_per_page' => -1, 'order_by' => 'date', 'order' => 'ASC'
);
$query = new WP_Query( $args );"]
Would love some nudges in the right direction!
Thank you :)
I did something similar not too long ago and used the following as far as the date & making sure it only pulled posts from "today" and in the future.
_EventStartDate=>$today
That was for pulling upcoming events for a custom post type. With that said, this code is for your scenario which is retrieving normal posts:
date=>$today
Here's my whole custom query that works to pull events but I'm stuck on trying to figure out how to pull from multiple categories as well:
post_type=tribe_events&tribe_events_cat=featured&post_status=publish&_EventStartDate=>$today&orderby=_EventStartDate&order=asc
Please follow up once you have a solution.
my question has to do with the pods framework plugin for wordpress sites. I am using pods version 2.2 and have been having trouble with the where parameter in the find() function.
I'd be suprised if I am the first person to encounter this problem but I have searched extensively and haven't found anyone providing an answer (or question at that).
Anyway, I'll give an example to highlight my problem.
Say I have a Bands Pod and a Records Pod and these two pods have a bi-directional multi-select relationship between them (that is, an n to n relationship). Hence a band can have numerous records and a record can have multiple bands. Further, the relationship exists between the fields BandsPod('records') and RecordsPod('bands').
Now, I can retrieve all records in the records pod like so (note the where is commented out):
$pods = pods('records');
$params = array(
'select' => 't.*',
'limit' => -1
//,'where' => '???'
);
$pods->find($params);
Then do whatever I want with them, e.g. template(), fetch(), etc.
My trouble occurs when I want to filter by band. What should my where statement be if I want to retrieve all records by band with id 1?
My view is that it should be something like this:
$params = array(
'select' => 't.*',
'limit' => -1,
'where' => '1 IN bands.id'
);
$pods->find($params);
However, this does not work (not that I especially expected it to).
It would also be desirable to know how this would work for filtering by multiple bands. E.g.
'where' => '(1, 2) IN bands.id'
As I said before, I've been trying to get this to work for some time and with little joy. I have managed to get it working but in a very ugly way. I.e.
Get bands from Bands Pod with relevant band ids,
Collect all ids of records from the bands pod records field,
Write params for Records Pod
Use the ids in the where statement to check they match with t.id,
$pods->find(); //where $pods=pods('records');
Thanks in advance for taking the time to read this and any answers you may give.
Cheers,
Joe
N.B. I know about the filters() function and it does not do what I'm after. I'd like stuff specifically for the where statement please.
You would want:
'where' => 'bands.id IN ( 1, 2 )'
If your bands are a custom post type, it would be:
'where' => 'bands.ID IN ( 1, 2 )'
I have a database with a simple one-to-many relationship that looks like this:
Tables Company Category
Rows ID ID
Name Name
Category_ID
I have a forms where I can add, edit and delete the company's or category's name, which works fine. Entering the category by ID also works, but is obviously terrible to use. What I want is a simple select element in the form to pick from an existing category.
The code I have to generate the <select> is:
$this->add(array(
'name' => 'Categorie',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'options' => $categories,
),
'options' => array(
'label' => 'Categorie',
),
));
From what I could find in the (extremely sparse) ZF2 documentation, I should be using a Hydrator to fetch data into the $categories variable, but I'm not sure where to go from there.
Any sort of example or tips would be well appreciated!
Here's a very good article about forms.
My solution is based on this tutorial and it is (almost) working well. I don't know if you use Doctrine for your project or not, but I think it would be a very good idea!
I also heavily use select elements. In the given fieldset, I generate value options for a select with this piece of code:
$opt= Registry::get('entityManager')->getRepository('My\Entity\Categories')
->getCategoriesForCombobox();
$this->get('category')->setValueOptions($opt);
If you (plan to) use Doctrine, you should read about repositories very carefully in the Doctrine documentation. If you use collections in the form, the topic about associations is also a must read.
I am creating a forum for my school. A student will be able to create many topics in a particular forum and other students are able to comment on it. The tricky part is that i have a feature where a student can have sub comment to a comment. Lemme tell you my simple table structure which i am using Mysql and developing in cakephp. Below is not the full structure.
User (id,name)
Forums(id,desc,date)
Topics(id,user_id,forum_id,title,content,date)
comments(id,user_id,topics_id,content,date,parent_id) *=> the parent_id referers where the subcomment belongs to.*
I used the cakePHP containable to get all toics and comments of a particular forum in my forum_controller:
$this->find('all',array(
'contain'=>array(
'User'=>array(
'fields' => array ('id','displayName','gender','email','profileImgBig')
),'Post'=>array(
'User'=>array(
'fields' => array('id','displayName','gender','email','profileImgBig')
),
'order' => array('Post.created'=>'DESC'),
)
),
'conditions'=>array('Topic.forum_id'=>$id),
'order' => array('Topic.datePosted'=>'DESC')
));
The above retrieves all forum->Topics->User & Topics->Comments->User.
How can i implement in a way that its retrieves all Comments and sub comments? Please help and advice me. Thank you! I am looking at tree behaviour but i am not sure how am i suppose to incorporate with my above find statement.
You can indeed use the TreeBehaviour in CakePHP for this. You'll need to add two more fields to your database (lft, rght) besides parent_id for this and activate it in the model for comments.
See the CakePHP manual for details, but after activating the behaviour you can use functions like Model::children() and Model::find('threaded') to find all comments that reside below a certain comment (e.g, that are subcomments).
I would usually first query the database for all topics, and then queue the Comment model for comments and subcomments using the Model::children() function. I wonder though, why would you want to load all topics and all comments within those topics, and not just 1 topic with comments?
Let's say I have 3 models: User, Region, Country.
User belongsTo Region
Region belongsTo Country
Each of these models is using the Containable behavior. I'm attempting to find users from the country with code 'US'. Here's what I'm attempting:
$users = $this->User->find('all', array(
'conditions' => array('Country.code' => 'US'),
'contain' => array('Region.Country'),
));
CakePHP is separating this into 2 queries:
First, it is SELECTing the ID's for all countries with code 'US'.
Then it is using those ID's to SELECT all users JOINing regions where region.country_id is in that list of country ID's previously retrieved.
As a result, I end up with SQL errors in my app since my conditions array contains a reference to Country.code, and the second query that Cake builds doesn't JOIN countries.
The best Cake solution I see is to build a sub-query as described in the Complex Find Conditions portion of the manual. However, this seems very convoluted, and is more of a hack than I would like to implement. Is there an easier way that I'm overlooking?
Nate Abele (former lead dev of CakePHP) wrote an article about doing ad hoc joins which might help.
Put the conditions in the contain as well. E.g:
$users = $this->User->find('all', array('contain' => array(
'Region' => array(
'Country' => array(
'conditions' => array('Country.code' => 'US'),
),
),
)));
See the second-to-last example in the Containable manual