Posts tagged Zend Framework
There has to be an easier way :(
Feb 18th
So I’m working on a project, and I want to find all of the rows that don’t have a row in it’s related table using Zend_Db_Table_Select… Simple right? Not really
. In theory this would be the query that I would write…
SELECT author.id FROM author LEFT JOIN project ON project.author_id = author.id WHERE project.author_id IS NULL ORDER BY RAND() LIMIT 1
After toying with Zend_Table for a while I’ve managed to figure out how to do it…
/**
* Fetches a random author who has not had a project assigned to them
* @return Ambigous <Zend_Db_Table_Row_Abstract, NULL>
*/
public function findRandomNull(){
$authorModel = $this->getDbTable();
$select = $authorModel->select()
->setIntegrityCheck(false)
->from('author')
->joinLeft('project', 'project.author_id = author.id', array())
->where('project.author_id IS NULL')
->order('RAND()');
return $authorModel->fetchRow($select);
}
Which will produce the following SQL
SELECT `author`.* FROM `author` LEFT JOIN `project` ON project.author_id = author.id WHERE (project.author_id IS NULL) ORDER BY RAND() ASC
Zend_Acl + Zend_Auth + Zend_Controller_Plugin = HAPPY!
Aug 3rd
Ok, so after playing around with Zend_Acl for what seems like forever I’ve finally cracked what feels like the perfect marriage between Zend_Acl, Zend_Auth and an application.
You may ask yourself, why the fuck is it so hard? The simple answer is this…
Zend_Aclwas designed in such a way that it does not require any particular backend technology such as a database or cache server for storage of the ACL data. Its complete PHP implementation enables customized administration tools to be built uponZend_Aclwith relative ease and flexibility.
Ease and flexibility my arse! This is the reason quite a few Zend Developers are just pulling their hair out day in day out. The documentation at zendframework.com by far is the most confusing I’ve ever seen and quite a bit of it is outdated. It’s actually easier reading through the API documentation and trolling through the code than it is reading this stuff. I truelly feel sorry for people using ZF in anything less than ZendStudio or an IDE without the code completion. Authentication and Authorisation are pretty key aspect of any application, if they made it simple for people to set up they wouldn’t need a 2 paragraph section saying how to store the data by serializing it. Any way, rant over!
The Solution
There are many ways to store the ACL data for persistence, but I think that’s the problem. As soon as you hear the words store and persistance the bells and whistles in your head are screaming at you telling you to store it in a database just like in CakePHP. For months I’d been trying to work out a way to do this until last night.
Why not put this in the application configuration (application.ini)? This is application configuration after all! What’s the point in taking a hit on your database every time you want to check authorisation and then having to write caching code to counteract that? It’s counterproductive and not cost effective (thanks Kennedy stole your phrase).
We can then read the configuration from ini file which contains all of our Resources and Roles.
To begin I set my self some conditions:
- Whatever the solution is, it has to be decoupled from the rest of the application apart from the models used for the user obviously.
- It has to be piss easy to implement
- It has to be so easy to implement my mum could do it!
Non-existant fields Zend_Db_Table_Abstract
Dec 20th
I’d run into a problem in the Zend Table where when inserting data from a form, if a field didn’t exist in the database ZF would crap out with an exception. Having to unset posted variables I didn’t need, meant more typing, more typing meant more unnecessary code.
I found the easiest way to get around this is by overriding the insert method in my extended App_Db_Table_Abstract class. Here’s what my new insert method looks like.
public function insert($data, $skip = true){
if($skip === true){
$oldData = $data;
$data = array();
foreach($this->info('metadata') as $key => $value){
if(isset($oldData[$key])){
$data[$key] = $oldData[$key];
}
}
}
parent::insert($data);
}
The method takes two parameters, the data ($data) and a Boolean value ($skip). Skip defaults to true, so I don’t need to add this parameter to every call to insert, it’s also there in case I don’t want to skip my data.
It then iterates through the table meta data, which contains the field names for the table, checks to see if the key exists in the data from the form. If the field exists in the array then it’s added to the new data array.
Finally the parent insert method is called, bobs your uncle.
In case you’re wondering, I’ve subclassed some of the Zend classes, this allows me to override methods to my liking.
Zend_Acl Db Storage
Jul 25th
I’ve spent the last few hours trying to extend Zend_Acl_Role_Registry and Zend_Acl to support Db storage, when it just hit me what I’m actually trying to do, and a much easier way to do it!
More >
Zend Framework and DoJo
Jul 23rd
I pretty much wet my self when ZF announced that they’d be integrating Dojo with the next iteration, and low and behold the first RC was made publicly available a few days ago. I literally dived in head first today, only to find the documentation was some what lacking.
I don’t think the documentation is quite complete, but hats off to ZF for taking a step in the right direction.
Here’s a quick tutorial on using Dojo’s container’s and pane’s with ZF. I’ve added line breaks so it fits into the old blog.