I'm trying to write a basic plugin for the Symfony based CMS Diem.
I'm trying to list of the child pages for the current page, which I have managed to do:
$page = $this->getPage();
$this->subpages = $page->getNode()->getChildren();
However, I'm unsure of the syntax to use in order to filter the child records with conditions.
I would actually like to get just the records where the is_active field == 1.
I've looked at the documentation and I think I need to use the setBaseQuery method, but I could really do with an example to get me started.
Can anyone help?
Any advice appreciated.
Thanks.
Use DQL and prefetch what you need. I strongly recommend using DQL for everything accept a simple find
Or you can simple get it from the repository using the build in "magic" functions.
Something like:
Doctrine::em()->getRepository('Models\SubPage')->findByIsActive(1);
Check out the documentation
Related
I'm pretty new to Laravel, and still exploring it's features and possibilities, and I've came across a strange issue that I don't quite understand.
First of all, I'm using Laravel 5.0 and I'm trying to make pagination of results that came from model alongside with sorting them.
Here is the code that works, but without sorting:
News::paginate(5);
And here is what I need, but doesn't work:
News::paginate(5)->orderByDesc('published_at');
I've tried doing it like so:
News::all()->sortByDesc('published_at')->paginate(5);
But then, when I call render() method inside view it throws error. I would like to use model's approach since it's handy and few more things depend on it, and not query builder method. Also, would like, if possible to avoid manual or any kind of custom pagination. Thanks
You should write this. This will solve your problem
News::orderBy('published_at','desc')->paginate(5);
I need to add IGNORE option to saveAll() method in CAKEPHP. I want exact impementation of this . Some other SO questions answered related to validations and isUnique() checking like that. I need to implement this conditions , don't care waht is going on behind .
Please advise me.
The only way that I think this would be possible, other than using the execute() function provide by your data source is if you extended the Mysql datasource class and changed the behaviour of the create() and renderStatement() functions to includes case where you want it to append the IGNORE SQL keyword.
You'd want to place the data source file in APP/Model/Datasource/ and have it extends Mysql (assuming you use CakePHP 2+). Have a look at the 2 functions above. I'd just create a case where if uses 'createignore' instead of 'create' when calling renderStatement() when a certain option is passed to create().
That said, there most likely is a way to achieve what you're trying to do without going through that much trouble. If you elaborate on what you're trying to do, I might be able to give you a better answer.
This is a follow-up to an earlier question here: CakePHP find statement for a sql query involving two models
I wasn't sure whether to add it on to there, but I figured this is a new question in itself. My apologies if this isn't correct!
Given a cakephp find subquery, is it possible to use paginate() on the results of the find? from the cake documentation, it seems that the paginate() function only works on Models, not on an array of data.
Thank you!
if you already have all the data in the array, I suggest output all that to your page (not the same as showing all the data) and use javascript to paginate it (just use a javascript grid plugin out there): more responsive app and less load on the server.
I was looking at Zend_Paginator in Zend Framework project using MVC, and it looks like an intersting tool.
It looks like we can give a select object as argument to the factory, it's interesting because it means that i should return a select object from my model, is it a good way to do ?
It seems to be a bit heavy to do this, since it won't be needed all the times...
I can also give an array, which could come as a result of my method model, but in the case where i have a lot of data, it could be complicated to retrieve all the data from my database each times.
How can i handle this ?
From the doc: http://framework.zend.com/manual/en/zend.paginator.usage.html
However, it is possible to directly supply a count or count query yourself. See the setRowCount() method in the DbSelect adapter for more information.
And
In the case of the Null adapter, in lieu of a data collection you must supply an item count to its constructor.
I would suggest doing the count yourself, and then manually setting it. That is, based upon the reading I just did. Also, the doc states that if you go the NULL route, you can provide an item-count (Integer) to the Paginator constructor instead - this seems a bit more reasonable than querying for the number with each request.
I have posted about this a few weeks ago. It is found here:
http://blog.ekini.net/2009/06/22/zend-framework-how-to-use-zend_paginator/
It is a pretty straight-forward tutorial. It starts with the form, then the controller, and goes down to the view and the paginator file.
Well, i finally found an interesting way to do.
First, i implemented a Domain Model pattern after a read on Matthew Weier O'Phinney's blog, who explains how to.
Then, i created my own adapter of Zend_Paginator, to agree with my Model.
It's the most interesting way to do, i've found until now.
Hypothetical situation. I want to populate/load a sales/order collection with every order where the grand_total is equal to the total_paid. I know I can use addFieldToFilter to filter by a specific value, but is it possible to use this method to filter by other database values. If not, is there any data access object in the Magento system that allows this.
$orders = Mage::getModel('sales/order');
$orders = $orders->getCollection();
$orders->addFieldToFilter('total_paid',Array('eq'=>30)); //would find all the orders that were 30
//syntax to find all the orders whose total_paid value is equal to it's grand_total attribute
//????????
The non-eav SQL concept I’m grasping for is:
SELECT * FROM Orders o WHERE o.total_paid = o.grand_total
Is this possible to do purely with object method calls, or do I need to do post-load filtering?
How do other ORM systems handle this?
I was waiting for a definite answer to this, but seeing as nothing's come up I might as well share what I've found out.
I traced addFieldToFilter and apparently every value you use ends up quoted so you can't really "hack" this by using the name of a column.
In fact I don't see any way to do this, at least not within this class.
Realistically I think you'll need to go with post-load filtering, at least for now. This of course is the very definition of inefficient code, but then again if computational efficiency was a priority, you wouldn't be using Magento in the first place...
Just had to address this in one of my projects and came across this question while searching for a better resolution. My hack looks like this:
$orders = Mage::getModel('sales/order')->getCollection();
$orders->getSelect()
->where('total_paid=grand_total');
It annoys me that such a basic functionality of databases is overlooked by Magento's ORM.
There's a "polite" workaround for that.
See my example below where I am trying to filter where created_at <= expire_at.
Mage::getSingleton('foo/bar')->getCollection()
->addExpressionFieldToSelect('expired', '(created_at >= expire_at)', ['created_at' , 'expire_at'])
->addFilter('(created_at >= expire_at)', 0)
It's stupid, but still solves the problem using the class.
PS: I hope that 11 years after publishing this question you've solved the problem. :D (Just a joke. I love your work.)