Re: [fw-general] Bootstrap Resource usage in model - best practice advice please
Does this means that you should have base class for all models with getCache method, and extend it? Regards, Saša Stamenković On Fri, Jan 15, 2010 at 1:52 AM, Hector Virgen wrote: > Hey Nick. I was asking myself the same question the other night. > > While it doesn't hurt to pass in your dependencies from within the > controller, it can quickly become tedious. I ended up going with a lazy-load > approach so keep my models unit-testable: > > public function getCache() > { > if (null === $this->_cache) { > $this->_cache = > Zend_Controller_Front::getInstance()->getParam('bootstrap')-> > getResource('Cache'); > } > return $this->_cache; > } > > This would allow you to easily create a setter method that your unit tests > can use to stub in a black hole cache. > > -- > Hector > > > > On Mon, Nov 16, 2009 at 11:55 PM, Саша Стаменковић wrote: > >> Hi. >> >> Zend_Controller_Front::getInstance()->getParam('bootstrap')-> >> getResource('Cache'); >> >> But I like >> >> $cache = Zend_Registry::get('Zend_Cache); >> >> it's shorter. >> >> Regards, >> Saša Stamenković >> >> >> >> On Tue, Nov 17, 2009 at 8:34 AM, Nick Pack wrote: >> >>> This is probably a dumb question, but I'm looking for a way to call a >>> bootstrap resource from a model (in this case is a Zend_Cache object). >>> >>> Bootstrap method: >>> >>>protected function _initCache() >>>{ >>>$frontendOptions = array( >>> 'lifetime' => 7200, // cache lifetime of 2 hours >>> 'automatic_serialization' => true >>>); >>> >>>$cachedir = realpath(/path/to/cache'); >>>$backendOptions = array( >>>'cache_dir' => $cachedir >>>); >>> >>>$cache = Zend_Cache::factory('Core', >>> 'File', >>> $frontendOptions, >>> $backendOptions); >>>Zend_Registry::set('Zend_Cache',$cache); >>>return $cache; >>>} >>> >>> Obviously this is set into the registry so I can access it that way, but >>> I was wondering if this is the best way to do it, or whether there was some >>> way of calling it like you do in a controller: >>> >>> $this->_cache = $this->getInvokeArg('bootstrap')->getResource('Cache'); >>> >>> or maybe even passing it directly over to the __construct of the model. >>> >>> Any advice greatly appreciated >>> >> >> >
Re: [fw-general] Bootstrap Resource usage in model - best practice advice please
Hey Nick. I was asking myself the same question the other night. While it doesn't hurt to pass in your dependencies from within the controller, it can quickly become tedious. I ended up going with a lazy-load approach so keep my models unit-testable: public function getCache() { if (null === $this->_cache) { $this->_cache = Zend_Controller_Front::getInstance()->getParam('bootstrap')-> getResource('Cache'); } return $this->_cache; } This would allow you to easily create a setter method that your unit tests can use to stub in a black hole cache. -- Hector On Mon, Nov 16, 2009 at 11:55 PM, Саша Стаменковић wrote: > Hi. > > Zend_Controller_Front::getInstance()->getParam('bootstrap')-> > getResource('Cache'); > > But I like > > $cache = Zend_Registry::get('Zend_Cache); > > it's shorter. > > Regards, > Saša Stamenković > > > > On Tue, Nov 17, 2009 at 8:34 AM, Nick Pack wrote: > >> This is probably a dumb question, but I'm looking for a way to call a >> bootstrap resource from a model (in this case is a Zend_Cache object). >> >> Bootstrap method: >> >>protected function _initCache() >>{ >>$frontendOptions = array( >> 'lifetime' => 7200, // cache lifetime of 2 hours >> 'automatic_serialization' => true >>); >> >>$cachedir = realpath(/path/to/cache'); >>$backendOptions = array( >>'cache_dir' => $cachedir >>); >> >>$cache = Zend_Cache::factory('Core', >> 'File', >> $frontendOptions, >> $backendOptions); >>Zend_Registry::set('Zend_Cache',$cache); >>return $cache; >>} >> >> Obviously this is set into the registry so I can access it that way, but I >> was wondering if this is the best way to do it, or whether there was some >> way of calling it like you do in a controller: >> >> $this->_cache = $this->getInvokeArg('bootstrap')->getResource('Cache'); >> >> or maybe even passing it directly over to the __construct of the model. >> >> Any advice greatly appreciated >> > >
[fw-general] Zend_Loader_Autoloader_Resource namespace with underscore character
hi again, I've been trying to use Zend_Application_Module_Autoloader recently with a namespace containing a _ such as: Module_User Now, the namespace appears to be set by Zend_Loader_Autoloader_Resource::setNamespace() which just takes the string, removes any trailing _ and assigns it to the _namespace property. That bit works fine. However, the autoload fails because Zend_Application_Module_Autoloader::getClassPath() checks to see whether the top namespace part (separated by _) matches the _namespace argument (l. 148). In my example this tries to match 'Module' with 'Module_User' and understandably fails. My question is, is Zend_Loader_Autoloader_Resource supposed to support namespaces which contain _ characters in the middle of the text? Or am I pushing things a little too far with this example? If namespaces shouldn't contain _ in the middle of the text can I suggest some validation is added to setNamespace() to make detecting this issue a little easier :) best wishes, Si
Re: [fw-general] Check if an image exists on remote server
Some servers support using the "HEAD" method in your request. It will allow you to check for the file without actually downloading the file. Many server support 'if-modified-since' which can be used with an late enough date to either get "unmodified" (file exists) or "not found" It appears Zend_Http_Client can be used with both methods to check for a file without actually downloading it. On Thu, Jan 14, 2010 at 1:24 PM, Fred Jiles wrote: > Not a ZF way to do it, but you could use curl, and get the response. If it > can't find it, it will be 404, 200 on found. Again this will only work if > accessible via http request. > > > On Thu, Jan 14, 2010 at 2:20 PM, Bruno Fajardo wrote: > >> 2010/1/14 JoostV >> > >> > I need to check whether an image exists on a remote server. Anybody got >> any >> > quick pointers? >> > >> > if (file_exists($file) && is_file($file)) { >> > } >> > only works for local files. >> >> Actually, as described in the PHP manual section for the file_exists >> function (http://www.php.net/manual/en/function.file-exists.php), as >> of PHP 5.0.0, this function can also be used with some URL wrappers, >> including HTTP. The image on the remote server is accessible via >> webserver? >> >> > -- >> > View this message in context: >> http://n4.nabble.com/Check-if-an-image-exists-on-remote-server-tp1014172p1014172.html >> > Sent from the Zend Framework mailing list archive at Nabble.com. >> > >
Re: [fw-general] Resource autoloading for modules via Zend_Application
Thanks Mon & Graham, Yes, once I'd added the module-specific Bootstrap the resource autoloader worked. However, that's not how I understood the documentation. The line: "By default, an instance of Zend_Application_Module_Autoloader will be created for the module, using the module name and directory to initialize it." could be read (by me at least!) that module autoloading is a separate feature of Zend_Application_Resource_Modules. I thought, incorrectly obviously, that enabling the module resource would support the autoloader. But I see now, you have to enable the ini setting AND have a Bootstrap to use the module bootstrap resource. What the documentation certainly doesn't say, and should, is that the module Bootstrap has to extend Zend_Application_Module_Bootstrap in order to make all this work. If that is correct, I may suggest some alterations to the module resource docs to make this clearer (perhaps a small code example would help). Should I send that to this list / zf-docs / or just post an issue on JIRA? best wishes, Simon I'm confused. Did it work or did it not work after creating a module bootstrap? Because that's how it's supposed to work. The module autoloader is registered by the bootstrap and not the modules resource plugin, so simply activating the plugin is not enough. If it didn't work, maybe your bootstrap class name is wrong. The class name should be _Bootstrap and the file should be named Bootstrap.php and live in the module directory. -- Mon
Re: [fw-general] Check if an image exists on remote server
Not a ZF way to do it, but you could use curl, and get the response. If it can't find it, it will be 404, 200 on found. Again this will only work if accessible via http request. On Thu, Jan 14, 2010 at 2:20 PM, Bruno Fajardo wrote: > 2010/1/14 JoostV > > > > I need to check whether an image exists on a remote server. Anybody got > any > > quick pointers? > > > > if (file_exists($file) && is_file($file)) { > > } > > only works for local files. > > Actually, as described in the PHP manual section for the file_exists > function (http://www.php.net/manual/en/function.file-exists.php), as > of PHP 5.0.0, this function can also be used with some URL wrappers, > including HTTP. The image on the remote server is accessible via > webserver? > > > -- > > View this message in context: > http://n4.nabble.com/Check-if-an-image-exists-on-remote-server-tp1014172p1014172.html > > Sent from the Zend Framework mailing list archive at Nabble.com. >
Re: [fw-general] Check if an image exists on remote server
2010/1/14 JoostV > > I need to check whether an image exists on a remote server. Anybody got any > quick pointers? > > if (file_exists($file) && is_file($file)) { > } > only works for local files. Actually, as described in the PHP manual section for the file_exists function (http://www.php.net/manual/en/function.file-exists.php), as of PHP 5.0.0, this function can also be used with some URL wrappers, including HTTP. The image on the remote server is accessible via webserver? > -- > View this message in context: > http://n4.nabble.com/Check-if-an-image-exists-on-remote-server-tp1014172p1014172.html > Sent from the Zend Framework mailing list archive at Nabble.com.
[fw-general] Check if an image exists on remote server
I need to check whether an image exists on a remote server. Anybody got any quick pointers? if (file_exists($file) && is_file($file)) { } only works for local files. -- View this message in context: http://n4.nabble.com/Check-if-an-image-exists-on-remote-server-tp1014172p1014172.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Resource autoloading for modules via Zend_Application
On Thursday 14 January 2010 13:37:02 Simon R Jones wrote: >to partially answer my own question "resources.modules[] =" did work, >not sure what I was doing. > >However, it appears only to work if I have a custom module Bootstrap, >is that expected behaviour since its not clear in the docs? Unless I'm >not reading it properly Oops... meant to send the following in the last mail. The module autoloaders inject themselves in the to the autoloader stack on instantiation so the following will add one with default resource configuration for each module. protected function _initModuleLoaders() { $this->bootstrap('frontcontroller'); $front = $this->getResource('frontcontroller'); $modules = array_keys($front->getControllerDirectory()); foreach($modules as $moduleName) { $basePath = APPLICATION_PATH . '/modules/' . $moduleName; $resourceLoader = new Zend_Application_Module_Autoloader(array( 'basePath' => $basePath, 'namespace' => ucfirst($moduleName), )); } }
Re: [fw-general] Resource autoloading for modules via Zend_Application
On Thursday 14 January 2010 13:37:02 Simon R Jones wrote: >to partially answer my own question "resources.modules[] =" did work, >not sure what I was doing. > >However, it appears only to work if I have a custom module Bootstrap, >is that expected behaviour since its not clear in the docs? Unless I'm >not reading it properly You could always do the following in the main bootstrap...
Re: [fw-general] Doctrine Counter Behavior
drm-4 wrote: > > bparise wrote: >> Ideally, these could all be under 1 "service" class... this way the >> "models" >> don't have anything defined in them other than the ORM stuff. >> >> ForumService::changeTopic(Post $post, Topic $newTopic) >> ForumService::savePost(Post $post) >> ForumService::deletePost(Post $post) >> ForumService::sendEmailNotifications(Topic $topic) >> >> What are your thoughts about this? I feel its a bit of redundancy since >> most of these methods are only going to be called within 1 place anyways. > I'm not sure if this is the info you wanted, but here goes: > > I think you should think about whether your application logic should be > worried about when and how these things need to be done. I don't really > think so. The application is only adding a post, whether your model > decides to precache the postcount per topic is up to the model. So you'd > use a service layer to tell your model to add a post, and the model > handles the rest. > > In the case of the post count, it makes sense to think about where you'd > let your application ask the model how many posts a topic has. Would you > let the service layer, or even the application layer, wonder about where > to get this info? The answer is no. You simply tell your model "get me > these topics, and I want to know the post count per topic". So, if the > application or service layer don't know how to get this info, why should > they know how to store it in the first place? > > One more piece of food-for-thought: > Let's say you didn't precache this info, but used a JOIN with a COUNT() > whenever you read topics from the database. Now you encounter problems > with performance and you decide to cache this info. In what part of your > application would you want to make changes? The obvious hint is that > we're talking about SQL here. > > Also note that Doctrine 1.x's Active Record pattern isn't really a good > practice for separating business model logic and persistence logic. > These two are intertwined in such a pattern, and it would (imho) > actually overcomplicate things trying to separate these more than > absolutely necessary. > > HTH > drm / Gerard > > You bring up some excellent points, and I would appreciate if we could further debate this since I am very interested in getting other people's opinions on this!!! Your argument about how a service layer should not delegate what to store to the model/db maybe is slightly incorrect. For example: Saving a Topic - Is the user logged in? - Does the user have permissions to post in the topic's forum? - Is anything about the topic spam, etc? - Is this user posting too many topics too fast (spam)? Reading a Topic - Just get the topic (all things above are assumed) As you can see, there are several factors that can play into the saving of a record vs. reading. My main issue is more or less, what *should* be in the model. This question probably has been debated since the inception of the MVC design pattern. In my example above, where should this logic be? Model: No. I think for obvious reasons (it just doesn't belong in the model especially things about user permissions) Controller: Maybe. But if you have multiple places that add topics (forum, admin, web services, etc) then you can run into the issue of redundancy. Plus, I *HATE* fat controllers. The "service" pattern is something that I just learned about recently. It really addresses the whole abstraction of an ENTIRE event, not just the straight DB call (like, save or delete). That's especially important in enterprise-level applications and even testing. If every "add topic" (web service, etc) calls that one service function, you only have to test that 1 service function and not 3 individual ones. Back to counter cache ... I refuse to do a "runtime" count of the posts. Not only because of the obvious performance issue, but IMO, it's downright sloppy. Probably the *best* way to handle the counter cache is through DB-level triggers. This way there is no doubt that the integrity of those "count" values are 100%. Plus, this would require no extra integration on the part of the model (although im sure doctrine does NOT support these)... posts_after_save: BEGIN UPDATE topics SET post_count = (post_count+1) WHERE id=NEW.topic_id END posts_after_delete: BEGIN UPDATE topics SET post_count = (post_count-1) WHERE id=OLD.topic_id END etc.. -- View this message in context: http://n4.nabble.com/Doctrine-Counter-Behavior-tp1010418p1014086.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Resource autoloading for modules via Zend_Application
I'm confused. Did it work or did it not work after creating a module bootstrap? Because that's how it's supposed to work. The module autoloader is registered by the bootstrap and not the modules resource plugin, so simply activating the plugin is not enough. If it didn't work, maybe your bootstrap class name is wrong. The class name should be _Bootstrap and the file should be named Bootstrap.php and live in the module directory. -- Mon On Thu, Jan 14, 2010 at 8:37 PM, Simon R Jones wrote: > to partially answer my own question "resources.modules[] =" did work, not > sure what I was doing. > > However, it appears only to work if I have a custom module Bootstrap, is > that expected behaviour since its not clear in the docs? Unless I'm not > reading it properly > > best wishes, > Si > > > > On 14 Jan 2010, at 12:02, Simon R Jones wrote: > > hi, >> >> I'm trying to get the resource autoloader working with Zend_App, my >> application.ini has: >> >> resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" >> resources.modules[] = >> >> And I'm expecting that a class called Test_Model_User would map to >> modules/test/models/User.php >> >> However, the class cannot be found. Looking at the code (v1.9.5) it >> appears that the resource autoloader is only instantiated for a module if >> that module has a custom Bootstrap which extends >> Zend_Application_Module_Bootstrap. Tried that, still nothing. >> >> Am a little confused, since the docs suggest simply this will do: >> resources.modules[] = >> >> Any help appreciated :) >> >> best wishes, >> Si >> >> > >
Re: [fw-general] Resource autoloading for modules via Zend_Application
to partially answer my own question "resources.modules[] =" did work, not sure what I was doing. However, it appears only to work if I have a custom module Bootstrap, is that expected behaviour since its not clear in the docs? Unless I'm not reading it properly best wishes, Si On 14 Jan 2010, at 12:02, Simon R Jones wrote: hi, I'm trying to get the resource autoloader working with Zend_App, my application.ini has: resources.frontController.moduleDirectory = APPLICATION_PATH "/ modules" resources.modules[] = And I'm expecting that a class called Test_Model_User would map to modules/test/models/User.php However, the class cannot be found. Looking at the code (v1.9.5) it appears that the resource autoloader is only instantiated for a module if that module has a custom Bootstrap which extends Zend_Application_Module_Bootstrap. Tried that, still nothing. Am a little confused, since the docs suggest simply this will do: resources.modules[] = Any help appreciated :) best wishes, Si
[fw-general] Resource autoloading for modules via Zend_Application
hi, I'm trying to get the resource autoloader working with Zend_App, my application.ini has: resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.modules[] = And I'm expecting that a class called Test_Model_User would map to modules/test/models/User.php However, the class cannot be found. Looking at the code (v1.9.5) it appears that the resource autoloader is only instantiated for a module if that module has a custom Bootstrap which extends Zend_Application_Module_Bootstrap. Tried that, still nothing. Am a little confused, since the docs suggest simply this will do: resources.modules[] = Any help appreciated :) best wishes, Si
Re: [fw-general] Caching DB relational values....
Hi, I'm beginning with Zend_Cache. I would like to ask your help on caching DB relational data in controller and view script. Easiest in this case is cache the output of your view script, and let the controller check for cache validity. if($cache->isValid(some id)) { show cached version } else { $this->view->assign('stuff', getStuff()) $contents = render view template $cache->store(some id, $contents) show $contents } You could implement an action helper for this. Another solution is use a view helper to cache the contents of the view script and make sure the controller uses a lazy-load for your database contents. This is more complex though, and imho not really preferable. HTH Gerard