web developing
28 Jul
Let’s say you have two models – each one modeling a database table – Users & Article. Here there’s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application.
What happens if you have to write a getUserArticles method? Where would you put it? Whether this will be the User model or the Article model?
Although technically you can put it in both models my advice is to look at the SQL query. If the FROM clause is containing the user table – than put the method in the User model, but here you’d have something like:
SELECT * FROM Article WHERE user_id = 1
I’d prefer to place it in the Article model!
27 Jul
Perhaps Master Yoda would say that, but it’s interesting to know what methodology do you use in your work. Any answers will be highly appreciated, so here are some basic questions:
Thanks anybody in advance!
22 Jul
Zend_Validate_Db_RecodExists is an extremely useful validator in Zend Framework when you’d like to be sure that a give row exists. Now it seems to be even better. Before you could check for a specific row by only comparing a value to the specified column:
$validator = new Zend_Validate_Db_RecordExists('db_table_name', 'column_name'); if ($validator->isValid(122)) { ... }
which made it useless when you’d like to compare by more than one column. Now this is changed and you can even exclude given rows by adding an exclude clause.
$validator = new Zend_Validate_Db_NoRecordExists( array( 'table' => 'users', 'field' => 'username', 'exclude' => array( 'field' => 'id', 'value' => $user_id ) ) );
21 Jul
In a large scale web application, especially based on Zend Framework, there are lot’s of components that support built in cache support. Such are Zend_Db, Zend_Translate, Zend_Date, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the “beginning”, into the bootstrap.php or even better – into a front controller plugin, and to store it into the Zend_Registry. Thus you’ve to change only the lifetime for specific needs:
<?php class CacheInit extends Zend_Controller_Plugin_Abstract { public function __construct() { $frontendOptions = array( 'automatic_serialization' => true, 'lifetime' => 60 ); $backendOptions = array( 'cache_dir' => realpath(APPLICATION_PATH . '/../cache') ); $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); Zend_Registry::set('cache', $cache); } }
Than add it as a front controller plugin:
// cache plugin $frontController->registerPlugin(new CacheInit());
20 Jul
There are so many tutorials about that, but let me add it one more time. You’ve to use the PDO_MYSQL adapter:
// where params include adapter, host // username, password and dbname $db = new Zend_Db_Adapter_Pdo_Mysql($params);
and set the default adapter
Zend_Db_Table::setDefaultAdapter($db);
That can be done in the bootstrap or into a front controller plugin!