Re: [fw-general] Zend_Rest_Controller - n00b question
Ask the PHP gods and they shall provide... A wealth of great information much appreciated. I look forward to the blog entries. E On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote: -- DorkFest ed...@metafoundry.com wrote (on Monday, 07 September 2009, 07:28 AM -0700): Is there any documentation on this component? Can this controller be used to build an XML/JSON based API service? If so, is there a built in method for retrieving an XML request fragment to the controller (ie: not key/val pairs)? I think this is normally access with something like: file_get_contents() from stdin. Wondering if ZF has a built-in way. You can pull in the raw request body from the request object: $body = $this-getRequest()-getRawBody(); With JSON data, it's trivial then to get your data: $data = Zend_Json::decode($body); With XML, simply pass it to SimpleXML: $data = new SimpleXMLElement($body); What I've done is to create an action helper that checks the Accept request header ($request-getHeader('Accept')), and then sets the format request parameter accordingly: // Accept headers often are of format: application/json; charset=utf-8 $accept = $request-getHeader('Accept'); $accept = explode(';', $accept); $accept = trim(array_shift($accept)); switch ($accept) { case application/json: $request-setParam('format', 'json'); break; case application/xml: $request-setParam('format', 'xml'); break; } Put the above code in the preDispatch() method of your action helper, and then register that helper with the helper broker in your bootstrap to ensure it's run for each controller requested. Then, in your controller, you can initialize the ContextSwitch helper: $context = $this-_helper-contextSwitch; $context-setAutoJsonSerialization(false); // do this so you can have // JSON-specific views $context-addActionContext('index', 'xml') -addActionContext('index', 'json') // ... $context-init(); If the context (xml or json, in the example above) is found (which will be based on the format set previously), then it will attempt to render a different view script: index.xml.phtml or index.json.phtml, respectively. This allows you to keep your controller lean, and have view-specific logic for each format. (For instance, I like to set my response headers, such as Content-Type, Content-Range, etc., within my views.) I'm surprised there are very few posts and almost no documentation. Does anyone have info on this component? Well, the documentation is primarily centered around the basic usage of the component, which is for creating RESTful resources in the MVC via a combination of routing and specific actions in your controller. How you handle different content types is something else entirely. I'll be blogging on some of this information in the coming months, and also covering it during my Architecting Ajax Applications in Zend Framework tutorial at ZendCon. -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/
Re: [fw-general] Zend_Rest_Controller - n00b question
Doesn't this component render Zend_Rest_Server moot? I'm sure I'm missing something, but Zend_Rest_Controller seems to provide a much better interface to managing REST contexts than Zend_Rest_Server as ZRC makes the route-to-context translation invisible. What did I miss? Thanks! Eddie On Sep 7, 2009, at 11:11 AM, Matthew Weier O'Phinney wrote: -- DorkFest ed...@metafoundry.com wrote (on Monday, 07 September 2009, 07:28 AM -0700): Is there any documentation on this component? Can this controller be used to build an XML/JSON based API service? If so, is there a built in method for retrieving an XML request fragment to the controller (ie: not key/val pairs)? I think this is normally access with something like: file_get_contents() from stdin. Wondering if ZF has a built-in way. You can pull in the raw request body from the request object: $body = $this-getRequest()-getRawBody(); With JSON data, it's trivial then to get your data: $data = Zend_Json::decode($body); With XML, simply pass it to SimpleXML: $data = new SimpleXMLElement($body); What I've done is to create an action helper that checks the Accept request header ($request-getHeader('Accept')), and then sets the format request parameter accordingly: // Accept headers often are of format: application/json; charset=utf-8 $accept = $request-getHeader('Accept'); $accept = explode(';', $accept); $accept = trim(array_shift($accept)); switch ($accept) { case application/json: $request-setParam('format', 'json'); break; case application/xml: $request-setParam('format', 'xml'); break; } Put the above code in the preDispatch() method of your action helper, and then register that helper with the helper broker in your bootstrap to ensure it's run for each controller requested. Then, in your controller, you can initialize the ContextSwitch helper: $context = $this-_helper-contextSwitch; $context-setAutoJsonSerialization(false); // do this so you can have // JSON-specific views $context-addActionContext('index', 'xml') -addActionContext('index', 'json') // ... $context-init(); If the context (xml or json, in the example above) is found (which will be based on the format set previously), then it will attempt to render a different view script: index.xml.phtml or index.json.phtml, respectively. This allows you to keep your controller lean, and have view-specific logic for each format. (For instance, I like to set my response headers, such as Content-Type, Content-Range, etc., within my views.) I'm surprised there are very few posts and almost no documentation. Does anyone have info on this component? Well, the documentation is primarily centered around the basic usage of the component, which is for creating RESTful resources in the MVC via a combination of routing and specific actions in your controller. How you handle different content types is something else entirely. I'll be blogging on some of this information in the coming months, and also covering it during my Architecting Ajax Applications in Zend Framework tutorial at ZendCon. -- Matthew Weier O'Phinney Project Lead| matt...@zend.com Zend Framework | http://framework.zend.com/
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
I use a custom validator for this functionality. Add it to the validation chain in the form model: $confirmPassword = new Zend_Form_Element_Password(password_confirm); $confirmPassword-setLabel('Confirm Password') -setDescription('Please re-enter your password.') -addValidator('PasswordConfirm') -addValidator('StringLength', false, array(5, 14)) -setAttribs(array('size'=30)) -setRequired(true); $this-addElement($confirmPassword); The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php. This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called password. I'm sure a generic one could be made for all confirms. ?php require_once 'Zend/Validate/Abstract.php'; class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; protected $_messageTemplates = array(self::NOT_MATCH = 'Passwords do not match.'); public function isValid($value, $context = null) { $value = (string) $value; $this-_setValue($value); if (is_array($context)) { if (isset($context['password']) ($value == $context['password'])) { return true; } } elseif (is_string($context) ($value == $context)) { return true; } $this-_error(self::NOT_MATCH); return false; } } Haven't tried the Dojo password box yet but looks cool. EH On Jan 5, 2009, at 12:14 PM, webPragmatist wrote: Is it possible to pass the POST variable(s) once instead of twice when using the identical validator? What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST). This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model). public function getAccountCreateForm($postPass = null) { // Add a password form element $password = new Zend_Dojo_Form_Element_PasswordTextBox('password'); $password-setLabel('Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password'); $form-addElement($password); // Add a confirm password form element $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox( 'confirmPassword'); $confirmPassword-setLabel('Confirm Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password') -addValidator('identical', true, array($postPass)); $form-addElement($confirmPassword); } View this message in context: Zend_Form / Identical Validator and comparing form fields Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
I set it in my form class. I don't know if there's a global way to set it, but i wouldn't doubt it. You could also use a base form class like so: class MF_Form { public function __construct($options = null) { parent::__construct($options); $this--setMethod('post') -addElementPrefixPath('MF_Filter', 'MF/Filter/', 'filter') -addElementPrefixPath('MF_Validate', 'MF/Validate/', 'validate'); } } class Form_Application extends MF_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('application_form') -setDescription('This is my form') -setAction(URL.'/merchant/account/apply') -setAttrib('id', 'application-form'); ...define elements... etc Good luck! EH On Jan 5, 2009, at 1:12 PM, webPragmatist wrote: Thanks! This works great... Quick question, I couldn't find it in the manual... a) is it required to set addPrefixPath() b) if so how can I set it globally? Edward Haber wrote: I use a custom validator for this functionality. Add it to the validation chain in the form model: $confirmPassword = new Zend_Form_Element_Password(password_confirm); $confirmPassword-setLabel('Confirm Password') -setDescription('Please re-enter your password.') -addValidator('PasswordConfirm') -addValidator('StringLength', false, array(5, 14)) -setAttribs(array('size'=30)) -setRequired(true); $this-addElement($confirmPassword); The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php. This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called password. I'm sure a generic one could be made for all confirms. ?php require_once 'Zend/Validate/Abstract.php'; class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; protected $_messageTemplates = array(self::NOT_MATCH = 'Passwords do not match.'); public function isValid($value, $context = null) { $value = (string) $value; $this-_setValue($value); if (is_array($context)) { if (isset($context['password']) ($value == $context['password'])) { return true; } } elseif (is_string($context) ($value == $context)) { return true; } $this-_error(self::NOT_MATCH); return false; } } Haven't tried the Dojo password box yet but looks cool. EH On Jan 5, 2009, at 12:14 PM, webPragmatist wrote: Is it possible to pass the POST variable(s) once instead of twice when using the identical validator? What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST). This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model). public function getAccountCreateForm($postPass = null) { // Add a password form element $password = new Zend_Dojo_Form_Element_PasswordTextBox('password'); $password-setLabel('Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password'); $form-addElement($password); // Add a confirm password form element $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox( 'confirmPassword'); $confirmPassword-setLabel('Confirm Password:') -setRequired(true) -setTrim(true) -setInvalidMessage('Please provide a valid password') -addValidator('identical', true, array($postPass)); $form-addElement($confirmPassword); } View this message in context: Zend_Form / Identical Validator and comparing form fields Sent from the Zend Framework mailing list archive at Nabble.com. -- View this message in context: http://www.nabble.com/Zend_Form---Identical-Validator-and-comparing-form-fields-tp21295103p21296134.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form / Identical Validator and comparing form fields
Sorry should have been : class MF_Form extends Zend_Form { Thx, EH
Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected?
Thanks for your help on this. It is much appreciated. I think it's pretty reasonable to think the construct if ($this- my_file-receive()) should return true only if the file is actually *received.* What else would a person conceivably be receiving? Whether or not the upload is required or optional this shouldn't effect it. Further, getFileName() shouldn't return the destination file-path when there is no upload *because there is no file*. Makes no sense. I had renamed manually because I need the product_id which I only have after gotten the last-insert-id from the save operation (my filenames are product_id.jpg). Is there a clever way to manage this from in the form that you know of? I would rather utilize the rename filter but I do not know the product-id until after validation. I tried (as you kindly/smugly suggested) the isUploaded hint but it lead to an error: if ($form-my_file-receive()) { if ($form-my_file-isUploaded()) { ... stuff ... } } Error: Method isUploaded does not exist. The way that worked for me was to check if getFileName() is a directory with is_dir(), a rather odd thing to have to do. However this worked. Thanks again! On Nov 12, 2008, at 2:30 AM, Thomas Weidner wrote: To clearify this: You are using trunk, but your code would not work. As you wrote, you upload no file, and call receive(). Receive can sometimes return true, for example when you set the file element it to be optional in the form, or when you set the ignoreNoFile option in the adapter. There are other ways to check this than relying on receive itself. Also to note: You rename manually and do not use the rename filter from Zend_File which means that YOU rename the file and not the component. Simply calling receive is not enough as you have noted. When you are doing implementations yourself you are also responsible of doing checks yourself. In your case a simple check if the file has been uploaded or not. :-)) Hint: isUploaded And as Matthew already noted: When someone uses trunk he should also read the manual of trunk. Online docs always refer to the latest stable release and not trunk. ;-) Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: Edward Haber [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Wednesday, November 12, 2008 3:28 AM Subject: Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected? I'm working off svn of the current trunk. On Nov 11, 2008, at 9:21 PM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Tuesday, 11 November 2008, 08:24 PM -0500): If no file is uploaded, receive() still returns true? Here's an example: Please test against trunk. The component has undergone a lot of work in the past two weeks, with basically daily check-ins. Additionally, read the current docbook documentation from trunk as well. if ($form-isValid($formData)) { ... do some stuff ... ... set $product_id to INT ... // process form image if ($form-product_image-receive()) { rename($form-product_image-getFileName(), AP . /media/products/ $product_id.jpg); } } With the above code, if no file is uploaded my destination directory gets renamed. So, if there is no file, receive() is still returning true and getFileName() is equal to the destination directory and consequently the whole directory gets renamed as 5.jpg (for example). Is this expected or am I implementing incorrectly?! I've asked some dumb questions on this list before. Hopefully this is another one. BTW, as someone else just noted the docs must be updated! Let me know if i can help. I know some HTML. Thx!! On Nov 11, 2008, at 4:55 PM, Simon Corless wrote: A quick question to Thomas I suppose, how do I get the file name / location from a file uploaded using Zend_Form. The docs contradict themselves... In the code it uses: $location = $form-foo-getFileName(); Then goes on to state in a note below: File values Within HTTP a file element has no value. Therefor you will get no output when calling getValue(). So I suppose it's a small bug in the docs, but ultimately I need the file name so how do I get it?! Thanks Simon - Simon http://www.ajb007.co.uk/ -- View this message in context: http://www.nabble.com/Zend_Form_Element_File---getValue%28%29-in-1.7-tp20449098p20449098.html Sent from the Zend Framework mailing list archive at Nabble.com. -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/
Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected?
It seems crystal clear to me that a method called getFileName should either return a filename or a null/false if there is no file. Otherwise the name of the method is a misnomer. When no file is present, getFileName returns the destination path (which is already accessible with getDestination). Why not have it return a current news headline or the random name of a Smurf instead? :-) In other words, why would people want the destination (which they could get from another method of the same class) to a file that was never uploaded? Thx! For the file transfer adapter this is logical. The adapter itself is built to support not only HTTP upload but also other protocols and directions. It could receive a complete directory and store it on another location. When you are using the file element as you wrote, you should stick with getValue(), as it returns null when there is no file or no upload and the filename when there was a upload, but without path. Regarding your filter problem: The normal usage in such a case would be... * validate form * add personal filter * receive the file Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: Edward Haber [EMAIL PROTECTED] To: Thomas Weidner [EMAIL PROTECTED] Cc: fw-general@lists.zend.com Sent: Wednesday, November 12, 2008 2:39 PM Subject: Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected? Thanks for your help on this. It is much appreciated. I think it's pretty reasonable to think the construct if ($this- my_file-receive()) should return true only if the file is actually *received.* What else would a person conceivably be receiving? Whether or not the upload is required or optional this shouldn't effect it. Further, getFileName() shouldn't return the destination file-path when there is no upload *because there is no file*. Makes no sense. I had renamed manually because I need the product_id which I only have after gotten the last-insert-id from the save operation (my filenames are product_id.jpg). Is there a clever way to manage this from in the form that you know of? I would rather utilize the rename filter but I do not know the product-id until after validation. I tried (as you kindly/smugly suggested) the isUploaded hint but it lead to an error: if ($form-my_file-receive()) { if ($form-my_file-isUploaded()) { ... stuff ... } } Error: Method isUploaded does not exist. The way that worked for me was to check if getFileName() is a directory with is_dir(), a rather odd thing to have to do. However this worked. Thanks again! On Nov 12, 2008, at 2:30 AM, Thomas Weidner wrote: To clearify this: You are using trunk, but your code would not work. As you wrote, you upload no file, and call receive(). Receive can sometimes return true, for example when you set the file element it to be optional in the form, or when you set the ignoreNoFile option in the adapter. There are other ways to check this than relying on receive itself. Also to note: You rename manually and do not use the rename filter from Zend_File which means that YOU rename the file and not the component. Simply calling receive is not enough as you have noted. When you are doing implementations yourself you are also responsible of doing checks yourself. In your case a simple check if the file has been uploaded or not. :-)) Hint: isUploaded And as Matthew already noted: When someone uses trunk he should also read the manual of trunk. Online docs always refer to the latest stable release and not trunk. ;-) Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: Edward Haber [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Wednesday, November 12, 2008 3:28 AM Subject: Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected? I'm working off svn of the current trunk. On Nov 11, 2008, at 9:21 PM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Tuesday, 11 November 2008, 08:24 PM -0500): If no file is uploaded, receive() still returns true? Here's an example: Please test against trunk. The component has undergone a lot of work in the past two weeks, with basically daily check-ins. Additionally, read the current docbook documentation from trunk as well. if ($form-isValid($formData)) { ... do some stuff ... ... set $product_id to INT ... // process form image if ($form-product_image-receive()) { rename($form-product_image-getFileName(), AP . /media/ products/ $product_id.jpg); } } With the above code, if no file is uploaded my destination directory gets renamed. So, if there is no file, receive() is still returning true and getFileName() is equal to the destination directory and consequently the whole directory gets renamed as 5.jpg (for example). Is this expected or am I
[fw-general] Zend_Form_Element_File - is this a BUG or expected?
If no file is uploaded, receive() still returns true? Here's an example: if ($form-isValid($formData)) { ... do some stuff ... ... set $product_id to INT ... // process form image if ($form-product_image-receive()) { rename($form-product_image-getFileName(), AP . /media/products/ $product_id.jpg); } } With the above code, if no file is uploaded my destination directory gets renamed. So, if there is no file, receive() is still returning true and getFileName() is equal to the destination directory and consequently the whole directory gets renamed as 5.jpg (for example). Is this expected or am I implementing incorrectly?! I've asked some dumb questions on this list before. Hopefully this is another one. BTW, as someone else just noted the docs must be updated! Let me know if i can help. I know some HTML. Thx!! On Nov 11, 2008, at 4:55 PM, Simon Corless wrote: A quick question to Thomas I suppose, how do I get the file name / location from a file uploaded using Zend_Form. The docs contradict themselves... In the code it uses: $location = $form-foo-getFileName(); Then goes on to state in a note below: File values Within HTTP a file element has no value. Therefor you will get no output when calling getValue(). So I suppose it's a small bug in the docs, but ultimately I need the file name so how do I get it?! Thanks Simon - Simon http://www.ajb007.co.uk/ -- View this message in context: http://www.nabble.com/Zend_Form_Element_File---getValue%28%29-in-1.7-tp20449098p20449098.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Form_Element_File - is this a BUG or expected?
I'm working off svn of the current trunk. On Nov 11, 2008, at 9:21 PM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Tuesday, 11 November 2008, 08:24 PM -0500): If no file is uploaded, receive() still returns true? Here's an example: Please test against trunk. The component has undergone a lot of work in the past two weeks, with basically daily check-ins. Additionally, read the current docbook documentation from trunk as well. if ($form-isValid($formData)) { ... do some stuff ... ... set $product_id to INT ... // process form image if ($form-product_image-receive()) { rename($form-product_image-getFileName(), AP . /media/products/ $product_id.jpg); } } With the above code, if no file is uploaded my destination directory gets renamed. So, if there is no file, receive() is still returning true and getFileName() is equal to the destination directory and consequently the whole directory gets renamed as 5.jpg (for example). Is this expected or am I implementing incorrectly?! I've asked some dumb questions on this list before. Hopefully this is another one. BTW, as someone else just noted the docs must be updated! Let me know if i can help. I know some HTML. Thx!! On Nov 11, 2008, at 4:55 PM, Simon Corless wrote: A quick question to Thomas I suppose, how do I get the file name / location from a file uploaded using Zend_Form. The docs contradict themselves... In the code it uses: $location = $form-foo-getFileName(); Then goes on to state in a note below: File values Within HTTP a file element has no value. Therefor you will get no output when calling getValue(). So I suppose it's a small bug in the docs, but ultimately I need the file name so how do I get it?! Thanks Simon - Simon http://www.ajb007.co.uk/ -- View this message in context: http://www.nabble.com/Zend_Form_Element_File---getValue%28%29-in-1.7-tp20449098p20449098.html Sent from the Zend Framework mailing list archive at Nabble.com. -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/
Re: [fw-general] Zend_Db
Thanks so much!! On Nov 7, 2008, at 8:21 AM, Matthew Weier O'Phinney wrote: I sent my original reply to you, but I'm posting here for the archives. -- Edward Haber [EMAIL PROTECTED] wrote (on Friday, 07 November 2008, 07:09 AM -0500): Thanks for the help! It is much appreciated. I had a feeling I was doing something a bit fishy. I tried the code below and the method is returning an associative array instead of an array of Article objects. If I do change the setFetchMode globally in the bootstrap, it changes to stdClass. Is there a way I can get this method to return an array of Article objects? Aha -- that was the missing bit. By default, if you call fetchAll() on the _table_ object, it returns a Zend_Db_Table_Rowset object, which is simply an iterable object of Row objects. In the class definition you presented, this would mean that it would return a rowset populated by Article objects. The following achieves that: return $this-fetchAll($select); Alternately, if you really, really want to have it return an array instead of a rowset, do the following: $rowset = $this-fetchAll($select); $return = array(); foreach ($rowset as $row) { $return[] = $row; } return $return; Personally, I'd just use the Rowset -- it acts just like an array for all practical intents and purposes. On Nov 6, 2008, at 9:35 PM, Matthew Weier O'Phinney wrote: -- DorkFest [EMAIL PROTECTED] wrote (on Thursday, 06 November 2008, 05:58 PM -0800): This was posted in Dec 2007. I have the same question. Is there anyway using fetchAll and setFetchMode to set the explicit class name that's returned in the array? Or is this a design conflict? Here's basically the situation: class Articles extends Zend_Db_Table_Abstract { protected $_name = articles; protected $_rowClass = Article; public function fetchActive() { $db = Zend_Registry::get('db'); $db-setFetchMode(Zend_Db::FETCH_OBJ); $select = $db-select() -from($this-_name) -where('active = 1') -order('creation_date DESC'); // want to return array of articles objects here instead of stdClass return $db-fetchAll($select); } } class Article extends Zend_Db_Table_Row_Abstract { } Is this the wrong way to approach models? Or is there a better way to obtain the fetchAll array of Articles as a return value from the fetchActive() method. Umm... wow... that code is.. wow. First, when you have a Zend_Db_Table object, you already *have* the database adapter -- there's no need to grab it from the registry. Next, Zend_Db_Table also has its own select object now (since 1.5) that obviates the need to (a) pull it from the database adapter, and (b) specify the table name (it's automatically populated). Next, fetchAll() on the adapter already returns an array by default -- it has from the very beginning. You actually have to override this behavior to get a different return type by passing the fetchmode as the second parameter to the call. The reason you're getting anything different is because your code is calling: $db-setFetchMode(Zend_Db::FETCH_OBJ); which tells the adapter to return fetch results as objects. You're basically shooting yourself in the foot here. So, the following will do what you're trying to accomplish: public function fetchActive() { $select = $this-select() -where('active = 1') -order('creation_date DESC'); return $this-getAdapter()-fetchAll($select); } Renan Gonçalves wrote: Hello, How I can use fetchAll with fethMode = Object and using the class Article (for example) ? I can use fetchObject('Article') and will fetch on my class, but in fetchAll the default class is stdClass. The PDOStatement has the function: ( http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) bool *setFetchMode* ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs ) -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/
Re: [fw-general] Zend_Db
Thanks for the help! It is much appreciated. I had a feeling I was doing something a bit fishy. I tried the code below and the method is returning an associative array instead of an array of Article objects. If I do change the setFetchMode globally in the bootstrap, it changes to stdClass. Is there a way I can get this method to return an array of Article objects? Eddie On Nov 6, 2008, at 9:35 PM, Matthew Weier O'Phinney wrote: -- DorkFest [EMAIL PROTECTED] wrote (on Thursday, 06 November 2008, 05:58 PM -0800): This was posted in Dec 2007. I have the same question. Is there anyway using fetchAll and setFetchMode to set the explicit class name that's returned in the array? Or is this a design conflict? Here's basically the situation: class Articles extends Zend_Db_Table_Abstract { protected $_name = articles; protected $_rowClass = Article; public function fetchActive() { $db = Zend_Registry::get('db'); $db-setFetchMode(Zend_Db::FETCH_OBJ); $select = $db-select() -from($this-_name) -where('active = 1') -order('creation_date DESC'); // want to return array of articles objects here instead of stdClass return $db-fetchAll($select); } } class Article extends Zend_Db_Table_Row_Abstract { } Is this the wrong way to approach models? Or is there a better way to obtain the fetchAll array of Articles as a return value from the fetchActive() method. Umm... wow... that code is.. wow. First, when you have a Zend_Db_Table object, you already *have* the database adapter -- there's no need to grab it from the registry. Next, Zend_Db_Table also has its own select object now (since 1.5) that obviates the need to (a) pull it from the database adapter, and (b) specify the table name (it's automatically populated). Next, fetchAll() on the adapter already returns an array by default -- it has from the very beginning. You actually have to override this behavior to get a different return type by passing the fetchmode as the second parameter to the call. The reason you're getting anything different is because your code is calling: $db-setFetchMode(Zend_Db::FETCH_OBJ); which tells the adapter to return fetch results as objects. You're basically shooting yourself in the foot here. So, the following will do what you're trying to accomplish: public function fetchActive() { $select = $this-select() -where('active = 1') -order('creation_date DESC'); return $this-getAdapter()-fetchAll($select); } Renan Gonçalves wrote: Hello, How I can use fetchAll with fethMode = Object and using the class Article (for example) ? I can use fetchObject('Article') and will fetch on my class, but in fetchAll the default class is stdClass. The PDOStatement has the function: ( http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) bool *setFetchMode* ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs ) Best Regards, -- Renan Gonçalves - Web Developer Cell Phone: +55 (11) 8633-6018 MSN: [EMAIL PROTECTED] Web Site: renangoncalves.com São Paulo - SP/Brazil -- View this message in context: http://www.nabble.com/Zend_Db-tp14140524p20373369.html Sent from the Zend Framework mailing list archive at Nabble.com. -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/
Re: [fw-general] Truncate String
I wrote a simple Truncate View Helper if you need truncations in the view script: http://www.zfhelpers.com/doku.php?id=repository:truncate It doesn't do words but it will add an HTML ellipsis character and truncate output to the desired length. On Oct 28, 2008, at 4:39 PM, Bradley Holt wrote: Matthew, On Tue, Oct 28, 2008 at 3:27 PM, Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: -- Bradley Holt [EMAIL PROTECTED] wrote (on Tuesday, 28 October 2008, 02:28 PM -0400): On Tue, Oct 28, 2008 at 1:12 PM, Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: -- Bradley Holt [EMAIL PROTECTED] wrote (on Tuesday, 28 October 2008, 12:29 PM -0400): Is there a simple way in ZF to truncate a string? I searched and didn't find any talk of a ZF specific component for this. I also looked at the list of standard filter classes and didn't see anything there either. I know there are tons of possible ways to do this - I'm just surprised there isn't anything in ZF yet so perhaps I'm just not looking in the right place. You're looking for Zend_Filter_StringTrim -- which can be used in the form classes as well. Maybe I'm just slow today, but I'm not sure how Zend_Filter_StringTrim would be used to truncate a string to a given character or word length. I thought it simply removed given characters from the beginning and end of a string. For example, I want to truncate the following: The quick brown fox jumped over the lazy sleeping dog. to 30 characters: The quick brown fox jumped ove or to 30 characters, but the closest whole word: The quick brown fox jumped or to 6 words (instead of characters): The quick brown fox jumped over Of course, there are probably other features that may be useful too. Is this something that can be done in Zend_Filter_StringTrim or using another ZF component? There are numerous ways to do this in PHP directly (so no need for anyone to post those here) but I was just curious if ZF had a clean and simple way to do this. Oh, never mind -- I was thinking truncate trailing whitespace, not truncate to a given preset length. Nope, nothing like that currently. Sounds like a good feature request. :) OK - that's what I thought. As you suggested, I've filed a feature request as ZF-4734: http://framework.zend.com/issues/browse/ZF-4734 Anyone else who would like to see this please go vote for it :-) Thanks, Bradley -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/ -- Bradley Holt [EMAIL PROTECTED]
Re: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2
I don't think it's documented yet but you can use: // full path + name $form-picture-getFileName() and // just the path $form-picture-getDestination() to get info about the uploaded file. On Oct 23, 2008, at 1:27 AM, andersl wrote: to me this And file elements do not have a value until now due to security reasons. means that getValue returns something now. so my question is now, when will getValue return something or is there another way to get the path of the uploaded file? andersl wrote: So that means if I get the trunk right now and remove the rename filter everything should be just fine? thomasW wrote: In trunk you don't have to add a empty rename filter. And file elements do not have a value until now due to security reasons. Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: andersl [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Tuesday, October 21, 2008 9:18 PM Subject: Re: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2 Hi I got the trunk from yesterday and even though I add the rename filter I still dont get any value from the getValue. The file gets uploaded but the getValue just dont return anything. I use addElements to get the element on the screen. Could this be the issue? My code looks something like this: ... $this-setAttrib('enctype', 'multipart/form-data'); ... //picture $picture = new Zend_Form_Element_File('picture'); $picture-setLabel('Picture') -setRequired(true) -setDestination('/var/www/images') -addValidator('Count', false, 1) // ensure only 1 file -addValidator('Size', false, 1MB) -addValidator('Extension', false, 'jpg,png,gif,jpeg') -addFilter('Rename', array('/var/www/images', true)) ; // only JPEG, PNG, and GIFs ... $submit = new Zend_Form_Element_Submit('submit'); $submit-setAttrib('id', 'submitbutton'); $this-addElements(array($id, $name, $picture, $submit)) and in my controller I have: if (!$form-picture-receive()) { print Error receiving the file; } //$row-picture = $form-picture-getValue(); //$row-picture = $form-getValue('picture'); both getValue gives me nothing :( Im looking forward to your reply :) Anders thomasW wrote: As reply to an mail this has been sent on 15.Oct: - Yes, this issue was added 7 hours ago and have already been fixed in trunk and branch. To solve this shortly you can: * Do not use addElement on the file element... the reason is that getValue() will be called on the file elements but they have no value and behave false in such a condition or * Add a empty rename filter by setting source and target the same value -- Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: drj201 [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Monday, October 20, 2008 1:21 PM Subject: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2 I am trying to add a Zend_Form_Element File to a form... I am extending Zend_Form with my own class for reuse purposes. The problem is that since upgrading to ZF 1.6.2 my code no longer works. Build element: $this-addElement('file', 'logo', array( 'label' = 'Logo', ...etc )); Add to form using group decorator: $this-addDisplayGroup( array('logo'), 'logodata', array( 'legend' = 'Dealer Logo' ) ); This results in the following error: Warning: Exception caught by form: Unknown file Stack Trace: #0 demoapp/lib/Zend/Form/Element/File.php(357): Zend_File_Transfer_Adapter_Abstract-getFilters('logo') #1 demoapp/lib/Zend/Form/Element.php(524): Zend_Form_Element_File-getFilters() #2 demoapp/lib/Zend/Form/Element.php(541): Zend_Form_Element-_filterValue(NULL, NULL) #3 demoapp/lib/Zend/Form/Decorator/ViewHelper.php(201): Zend_Form_Element-getValue() #4 demoapp/lib/Zend/Form/Decorator/ViewHelper.php(231): Zend_Form_Decorator_ViewHelper- getValue(Object(Zend_Form_Element_File)) #5 demoapp/lib/Zend/Form/Element.php(1905): Zend_Form_Decorator_ViewHelper-render('') #6 demoapp/lib/Zend/Form/Decorator/FormElements.php(100): Zend_Form_Element-render() #7 demoapp/lib/Zend/Form.php(2596): Zend_Form_Decorator_FormElements-render('') #8 /Applicat in demoapp/lib/Zend/Form.php on line 2616 Using the following manual approach works fine: $element = new Zend_Form_Element_File('foo'); $element-setLabel('Upload an image:') ...etc Any reason this has happened since upgrading to 1.6.2? Thanks, David -- View this message in context: http://www.nabble.com/Problems-with-Zend_Form_Element_File-since-ZF1.6.2-tp20067249p20067249.html Sent from the Zend Framework mailing list archive at Nabble.com. -- View this message in context: http://www.nabble.com/Problems-with-Zend_Form_Element_File-since-ZF1.6.2-tp20067249p20097147.html Sent
Re: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2
I don't think it's documented yet but you can use: // full path + name $form-picture-getFileName() and // just the path $form-picture-getDestination() to get info about the uploaded file. On Oct 23, 2008, at 1:27 AM, andersl wrote: to me this And file elements do not have a value until now due to security reasons. means that getValue returns something now. so my question is now, when will getValue return something or is there another way to get the path of the uploaded file? andersl wrote: So that means if I get the trunk right now and remove the rename filter everything should be just fine? thomasW wrote: In trunk you don't have to add a empty rename filter. And file elements do not have a value until now due to security reasons. Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: andersl [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Tuesday, October 21, 2008 9:18 PM Subject: Re: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2 Hi I got the trunk from yesterday and even though I add the rename filter I still dont get any value from the getValue. The file gets uploaded but the getValue just dont return anything. I use addElements to get the element on the screen. Could this be the issue? My code looks something like this: ... $this-setAttrib('enctype', 'multipart/form-data'); ... //picture $picture = new Zend_Form_Element_File('picture'); $picture-setLabel('Picture') -setRequired(true) -setDestination('/var/www/images') -addValidator('Count', false, 1) // ensure only 1 file -addValidator('Size', false, 1MB) -addValidator('Extension', false, 'jpg,png,gif,jpeg') -addFilter('Rename', array('/var/www/images', true)) ; // only JPEG, PNG, and GIFs ... $submit = new Zend_Form_Element_Submit('submit'); $submit-setAttrib('id', 'submitbutton'); $this-addElements(array($id, $name, $picture, $submit)) and in my controller I have: if (!$form-picture-receive()) { print Error receiving the file; } //$row-picture = $form-picture-getValue(); //$row-picture = $form-getValue('picture'); both getValue gives me nothing :( Im looking forward to your reply :) Anders thomasW wrote: As reply to an mail this has been sent on 15.Oct: - Yes, this issue was added 7 hours ago and have already been fixed in trunk and branch. To solve this shortly you can: * Do not use addElement on the file element... the reason is that getValue() will be called on the file elements but they have no value and behave false in such a condition or * Add a empty rename filter by setting source and target the same value -- Greetings Thomas Weidner, I18N Team Leader, Zend Framework http://www.thomasweidner.com - Original Message - From: drj201 [EMAIL PROTECTED] To: fw-general@lists.zend.com Sent: Monday, October 20, 2008 1:21 PM Subject: [fw-general] Problems with Zend_Form_Element_File since ZF1.6.2 I am trying to add a Zend_Form_Element File to a form... I am extending Zend_Form with my own class for reuse purposes. The problem is that since upgrading to ZF 1.6.2 my code no longer works. Build element: $this-addElement('file', 'logo', array( 'label' = 'Logo', ...etc )); Add to form using group decorator: $this-addDisplayGroup( array('logo'), 'logodata', array( 'legend' = 'Dealer Logo' ) ); This results in the following error: Warning: Exception caught by form: Unknown file Stack Trace: #0 demoapp/lib/Zend/Form/Element/File.php(357): Zend_File_Transfer_Adapter_Abstract-getFilters('logo') #1 demoapp/lib/Zend/Form/Element.php(524): Zend_Form_Element_File-getFilters() #2 demoapp/lib/Zend/Form/Element.php(541): Zend_Form_Element-_filterValue(NULL, NULL) #3 demoapp/lib/Zend/Form/Decorator/ViewHelper.php(201): Zend_Form_Element-getValue() #4 demoapp/lib/Zend/Form/Decorator/ViewHelper.php(231): Zend_Form_Decorator_ViewHelper- getValue(Object(Zend_Form_Element_File)) #5 demoapp/lib/Zend/Form/Element.php(1905): Zend_Form_Decorator_ViewHelper-render('') #6 demoapp/lib/Zend/Form/Decorator/FormElements.php(100): Zend_Form_Element-render() #7 demoapp/lib/Zend/Form.php(2596): Zend_Form_Decorator_FormElements-render('') #8 /Applicat in demoapp/lib/Zend/Form.php on line 2616 Using the following manual approach works fine: $element = new Zend_Form_Element_File('foo'); $element-setLabel('Upload an image:') ...etc Any reason this has happened since upgrading to 1.6.2? Thanks, David -- View this message in context: http://www.nabble.com/Problems-with-Zend_Form_Element_File-since-ZF1.6.2-tp20067249p20067249.html Sent from the Zend Framework mailing list archive at Nabble.com. -- View this message in context: http://www.nabble.com/Problems-with-Zend_Form_Element_File-since-ZF1.6.2-tp20067249p20097147.html Sent from the Zend
[fw-general] EAV
A couple months ago, php|architect ran a great cover story on EAV architectures. The article included a complete EAV implementation. Though, like all code from a magazine, it's likely to have a couple bugs and not be production-tested or fully secure. Magento has a great EAV system but I expect it's not loosely coupled (correct me if I'm wrong if the code can be used in other apps without the whole Magento base). Does anyone know of any production-ready EAV libraries in Zend Framework? I didn't see one in PEAR. Thx, Eddie
Re: [fw-general] PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64
Here are two possible solutions that occurred to me (both untested): class My_Dojo_Form extends Zend_Dojo_Form { public function setDescriptionDecorators() { $this-setElementDecorators(array( 'ViewHelper', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) ), $this-getDojoElements(), false); $this-setElementDecorators(array( 'DijitElement', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) ), $this-getDojoElements(), true); } public function getDojoElements() { $elements = array(); foreach ($this-getElements() as $name = $element) { if (isset($element-dijitParams)) { $elements[] = $name; } } return $elements; } } Another more succinct approach: class My_Dojo_Form extends Zend_Dojo_Form { public function setDescriptionDecorators() { $elements = array(); foreach ($this-getElements() as $name = $element) { $element-setDecorators(array( isset($element-dijitParams) ? 'DijitElement' : 'ViewHelper', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) )); } return $elements; } } Is one of these approaches better? Or is there a better approach? Eddie On Oct 12, 2008, at 11:14 AM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Sunday, 12 October 2008, 08:48 AM -0400): I noticed this problem when using the normal Zend Form Element stack of decorators on a form a that a dijit element in it (specifically it was the DateTextBox). For example, I was trying to add Description decorators to my Zend_Dojo_Form. The form is a mix of regular Zend_Form elements and Dojo elements. If you put a global elements decorator declaration at the end of the element declarations like so: class forms_AdminArticleForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('id', 'article-form'); ... declare some elements here ... $this-setElementDecorators(array( 'ViewHelper', Here's the problem right here. The Dijit form elements all use the DijitElement decorator as their base decorator. This is because the dijit view helpers have a different signature than other form view helpers (in order to accomodate a separation between dijit parameters and HTML attributes). Using setElementDecorators() on a mixture of Zend_Form and Zend_Dojo_Form elements is going to be problematic as a result. 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), )); } } It will trigger the exact error you mention. What I did was remove global decorators for the form and set decorators on a per element basis (only on the form elements that needed a description field). For Dojo elements, the decorator stack is slightly different: /** * creationdate */ $this-addElement('DateTextBox', 'creationdate', array( 'label' = 'Creation Date', 'description' = 'Leave blank for current date', 'decorators' = array( 'DijitElement', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), ) )); This fixed the problem but I had that nagging feeling something deeper must not be right either with the way I was approach my form, something I hadn't included, or some problem with ZF. For one thing, it would be nice to be able to describe one set of element decorators for all the form elements at once
Fwd: [fw-general] PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64
Oops, meant the description decorator (not directions). Just to be clear, could there be two new methods: $this-setZendElementDecorators(array(..etc...)); $this-setDojoElementDecorators(array(..etc...)); Begin forwarded message: From: Edward Haber [EMAIL PROTECTED] Date: October 12, 2008 11:34:21 AM EDT To: fw-general@lists.zend.com Subject: Re: [fw-general] PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64 There should be a way to globally change the Zend_Form decorators and as well globally set/change the Dojo decorators (either with one method or two methods this could be done). Otherwise, if you have a form with 25 elements and all of them have, say something simple like directions added (the directions decorator) then you have to declare 5 or 6 lines of default decorators for each element. That's 5 x 25 = 125 extra lines of default looking code added to your poor extended Zend_Form class. My $.02, Eddie On Oct 12, 2008, at 11:14 AM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Sunday, 12 October 2008, 08:48 AM -0400): I noticed this problem when using the normal Zend Form Element stack of decorators on a form a that a dijit element in it (specifically it was the DateTextBox). For example, I was trying to add Description decorators to my Zend_Dojo_Form. The form is a mix of regular Zend_Form elements and Dojo elements. If you put a global elements decorator declaration at the end of the element declarations like so: class forms_AdminArticleForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('id', 'article-form'); ... declare some elements here ... $this-setElementDecorators(array( 'ViewHelper', Here's the problem right here. The Dijit form elements all use the DijitElement decorator as their base decorator. This is because the dijit view helpers have a different signature than other form view helpers (in order to accomodate a separation between dijit parameters and HTML attributes). Using setElementDecorators() on a mixture of Zend_Form and Zend_Dojo_Form elements is going to be problematic as a result. 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), )); } } It will trigger the exact error you mention. What I did was remove global decorators for the form and set decorators on a per element basis (only on the form elements that needed a description field). For Dojo elements, the decorator stack is slightly different: /** * creationdate */ $this-addElement('DateTextBox', 'creationdate', array( 'label' = 'Creation Date', 'description' = 'Leave blank for current date', 'decorators' = array( 'DijitElement', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), ) )); This fixed the problem but I had that nagging feeling something deeper must not be right either with the way I was approach my form, something I hadn't included, or some problem with ZF. For one thing, it would be nice to be able to describe one set of element decorators for all the form elements at once (since Zend_Form and Zend_Dojo_Form are supposed to be someone interchangeable). Eddie On Oct 12, 2008, at 4:32 AM, Benjamin Eberlei wrote: On Sunday 12 October 2008 01:36:27 rswarthout wrote: When I try to use any Dojo enabled field I receive an error that just does not make any sense. PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64 Any insight would be greatly appreciated. Robert are you ultimately using the view helper or the form element? 1.) if using the view helper, you have to specify $this- dateTextBox($id, $value, array(), array()); if you want to pass no information to the attribs or dijit fields. $this-dateTextBox($id, $value, null, null); is not allowed 2.) if you are using Form: are you setting 'attribs' or 'dijitParams
Re: [fw-general] PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64
I noticed this problem when using the normal Zend Form Element stack of decorators on a form a that a dijit element in it (specifically it was the DateTextBox). For example, I was trying to add Description decorators to my Zend_Dojo_Form. The form is a mix of regular Zend_Form elements and Dojo elements. If you put a global elements decorator declaration at the end of the element declarations like so: class forms_AdminArticleForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('id', 'article-form'); ... declare some elements here ... $this-setElementDecorators(array( 'ViewHelper', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), )); } } It will trigger the exact error you mention. What I did was remove global decorators for the form and set decorators on a per element basis (only on the form elements that needed a description field). For Dojo elements, the decorator stack is slightly different: /** * creationdate */ $this-addElement('DateTextBox', 'creationdate', array( 'label' = 'Creation Date', 'description' = 'Leave blank for current date', 'decorators' = array( 'DijitElement', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), ) )); This fixed the problem but I had that nagging feeling something deeper must not be right either with the way I was approach my form, something I hadn't included, or some problem with ZF. For one thing, it would be nice to be able to describe one set of element decorators for all the form elements at once (since Zend_Form and Zend_Dojo_Form are supposed to be someone interchangeable). Eddie On Oct 12, 2008, at 4:32 AM, Benjamin Eberlei wrote: On Sunday 12 October 2008 01:36:27 rswarthout wrote: When I try to use any Dojo enabled field I receive an error that just does not make any sense. PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64 Any insight would be greatly appreciated. Robert are you ultimately using the view helper or the form element? 1.) if using the view helper, you have to specify $this- dateTextBox($id, $value, array(), array()); if you want to pass no information to the attribs or dijit fields. $this-dateTextBox($id, $value, null, null); is not allowed 2.) if you are using Form: are you setting 'attribs' or 'dijitParams' = null in the configuration of the form element? this leads to the problem described above. you ahve to set it to = array() to explicity stating its emptiness. greetings Benjamin -- Benjamin Eberlei http://www.beberlei.de
Re: [fw-general] PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64
There should be a way to globally change the Zend_Form decorators and as well globally set/change the Dojo decorators (either with one method or two methods this could be done). Otherwise, if you have a form with 25 elements and all of them have, say something simple like directions added (the directions decorator) then you have to declare 5 or 6 lines of default decorators for each element. That's 5 x 25 = 125 extra lines of default looking code added to your poor extended Zend_Form class. My $.02, Eddie On Oct 12, 2008, at 11:14 AM, Matthew Weier O'Phinney wrote: -- Edward Haber [EMAIL PROTECTED] wrote (on Sunday, 12 October 2008, 08:48 AM -0400): I noticed this problem when using the normal Zend Form Element stack of decorators on a form a that a dijit element in it (specifically it was the DateTextBox). For example, I was trying to add Description decorators to my Zend_Dojo_Form. The form is a mix of regular Zend_Form elements and Dojo elements. If you put a global elements decorator declaration at the end of the element declarations like so: class forms_AdminArticleForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('id', 'article-form'); ... declare some elements here ... $this-setElementDecorators(array( 'ViewHelper', Here's the problem right here. The Dijit form elements all use the DijitElement decorator as their base decorator. This is because the dijit view helpers have a different signature than other form view helpers (in order to accomodate a separation between dijit parameters and HTML attributes). Using setElementDecorators() on a mixture of Zend_Form and Zend_Dojo_Form elements is going to be problematic as a result. 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), )); } } It will trigger the exact error you mention. What I did was remove global decorators for the form and set decorators on a per element basis (only on the form elements that needed a description field). For Dojo elements, the decorator stack is slightly different: /** * creationdate */ $this-addElement('DateTextBox', 'creationdate', array( 'label' = 'Creation Date', 'description' = 'Leave blank for current date', 'decorators' = array( 'DijitElement', 'Description', 'Errors', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')), ) )); This fixed the problem but I had that nagging feeling something deeper must not be right either with the way I was approach my form, something I hadn't included, or some problem with ZF. For one thing, it would be nice to be able to describe one set of element decorators for all the form elements at once (since Zend_Form and Zend_Dojo_Form are supposed to be someone interchangeable). Eddie On Oct 12, 2008, at 4:32 AM, Benjamin Eberlei wrote: On Sunday 12 October 2008 01:36:27 rswarthout wrote: When I try to use any Dojo enabled field I receive an error that just does not make any sense. PHP Catchable fatal error: Argument 4 passed to Zend_Dojo_View_Helper_DateTextBox::dateTextBox() must be an array, null given in Zend/Dojo/View/Helper/DateTextBox.php on line 64 Any insight would be greatly appreciated. Robert are you ultimately using the view helper or the form element? 1.) if using the view helper, you have to specify $this- dateTextBox($id, $value, array(), array()); if you want to pass no information to the attribs or dijit fields. $this-dateTextBox($id, $value, null, null); is not allowed 2.) if you are using Form: are you setting 'attribs' or 'dijitParams' = null in the configuration of the form element? this leads to the problem described above. you ahve to set it to = array() to explicity stating its emptiness. greetings Benjamin -- Benjamin Eberlei http://www.beberlei.de -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/
Re: [fw-general] Toggle file-upload element with uploaded image
Here's the solution I came up with. Thanks for all the input and help on this problem. This solution involves using a conditional statement in Zend_Form to create one of two element types. When the image is present the view helper decorator replaces the form element with the image and a link to a remove action. The view helper is below and will still be tweaked a bit but it should give the overall idea. This form class snippet has all the other elements removed from it for clarity class MyForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $db = Zend_Registry::get('db'); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('enctype', 'multipart/form-data') -setAttrib('id', 'article-form'); /** * image_name */ if ($options['image_id']) { $this-addElement('file', 'image_name', array( 'label' = 'Image', 'description' = 'To upload another photo delete this one first.', 'attribs' = array( 'imgSrc' = URL . '/media/articles/' . $options['image_id'] . '.jpg', 'action' = URL . '/admin/article/drop-image/' . $options['image_id'] ), 'decorators' = array( array('ViewHelper', array('helper' = 'mediaImage')), 'Errors', 'Description', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) ) )); } else { $this-addElement('file', 'image_name', array( 'label' = 'Upload an image', 'destination' = AP . '/application/tmp/upload-staging', 'required' = true, 'validators' = array( array('Size', '500Kb'), array('ImageSize', array(10, 10, 1024, 768)), array('Extension', array('jpg', 'jpeg')) ) )); } (other form elements removed) Then I use this helper: class MF_View_Helper_MediaImage { public function mediaImage($fieldname, $value = , $attribs = null) { $imgSrc = $attribs['imgSrc']; $action = $attribs['action']; $class = isset($attribs['class']) ? $attribs['class'] : 'image- remove-button'; return OUTPUT img src=$imgSrc border=0 / | a href=$action class=$classDelete/a OUTPUT; } } This will pretty much be a reusable solution for the four CMS media subdirectories of image content and seems to work just fine. Happy for any comments. EH On Oct 3, 2008, at 8:12 AM, Matthew Lurz wrote: I would keep the image separate from the form unless you have a good reason not to. Then the logic is simple. Display the image if it exists, if not then display the form. Of course you would need an action to delete the image so that the form could be redisplayed. Edward Haber wrote: Definitely view logic. Good call. Still I'm uncertain though because I'm working with an extended Zend_Form class. Perhaps I should make a custom Form Element? Or a custom Form Decorator? Or use the view helper decorator for just that element? I'd love to hear the Zend best practice take on this solution. Thanks! On Oct 2, 2008, at 10:50 PM, Matthew Lurz wrote: Sounds like view logic to me and so I would add a conditional in the view script. Edward Haber wrote: I have a form that I want offer an image upload form element on. This has worked great using Zend_Form_Element_File. The upload works fine. What is the recommended way to have the uploaded image replace the upload form if an image exists? I don't need the solution coded, just the direction I should go. For example, I have the form as a class. SHould I pass the image name (if one exists) in the config array to the form? In the form i would then check for $config['imageName'] and if it exists display the image in place of the form
Re: [fw-general] Toggle file-upload element with uploaded image
That does sound like a better solution to me. Also, the link in the form could frustrate some users if they've partially completed the form then delete the image. Thanks for your comments. So then the best solution appears to be a decorator. On Oct 3, 2008, at 1:28 PM, Matthew Lurz wrote: In that case why not forego the remove action and the additional form logic and just display the form regardless of whether the image exists. When the user uploads a new image it would replace the existing one. Just wondering since it seems like the user would have to jump through an extra hoop to remove the image in order to upload a new one. The less a user does or has to do the better. Edward Haber wrote: Here's the solution I came up with. Thanks for all the input and help on this problem. This solution involves using a conditional statement in Zend_Form to create one of two element types. When the image is present the view helper decorator replaces the form element with the image and a link to a remove action. The view helper is below and will still be tweaked a bit but it should give the overall idea. This form class snippet has all the other elements removed from it for clarity class MyForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $db = Zend_Registry::get('db'); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('enctype', 'multipart/form-data') -setAttrib('id', 'article-form'); /** * image_name */ if ($options['image_id']) { $this-addElement('file', 'image_name', array( 'label' = 'Image', 'description' = 'To upload another photo delete this one first.', 'attribs' = array( 'imgSrc' = URL . '/media/articles/' . $options['image_id'] . '.jpg', 'action' = URL . '/admin/article/drop-image/' . $options['image_id'] ), 'decorators' = array( array('ViewHelper', array('helper' = 'mediaImage')), 'Errors', 'Description', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) ) )); } else { $this-addElement('file', 'image_name', array( 'label' = 'Upload an image', 'destination' = AP . '/application/tmp/upload-staging', 'required' = true, 'validators' = array( array('Size', '500Kb'), array('ImageSize', array(10, 10, 1024, 768)), array('Extension', array('jpg', 'jpeg')) ) )); } (other form elements removed) Then I use this helper: class MF_View_Helper_MediaImage { public function mediaImage($fieldname, $value = , $attribs = null) { $imgSrc = $attribs['imgSrc']; $action = $attribs['action']; $class = isset($attribs['class']) ? $attribs['class'] : 'image- remove-button'; return OUTPUT $imgSrc | $action Delete OUTPUT; } } This will pretty much be a reusable solution for the four CMS media subdirectories of image content and seems to work just fine. Happy for any comments. EH On Oct 3, 2008, at 8:12 AM, Matthew Lurz wrote: I would keep the image separate from the form unless you have a good reason not to. Then the logic is simple. Display the image if it exists, if not then display the form. Of course you would need an action to delete the image so that the form could be redisplayed. Edward Haber wrote: Definitely view logic. Good call. Still I'm uncertain though because I'm working with an extended Zend_Form class. Perhaps I should make a custom Form Element? Or a custom Form Decorator? Or use the view helper decorator for just that element? I'd love to hear
Re: [fw-general] Toggle file-upload element with uploaded image
There are a lot of other elements in the form though so it's not either the image or the whole form. The file upload is just one of 10 elements. It seems like an element decorator should work. But make a custom one or utilize a built-in one? I don't want to take over the directions decorator for example because that can't be best form. On Oct 3, 2008, at 3:19 PM, Matthew Lurz wrote: Sorry, that didn't come through quite right. See http://www.paste2.org/p/81737 instead. Matthew Lurz wrote: I wouldn't include the image in the form at all actually. Instead I would just wrap a conditional around a div in the view script. Something like: ? if (this-image): ? div ?= $this- image-src ?/div ? endif ? ?= $this-form ? Edward Haber wrote: That does sound like a better solution to me. Also, the link in the form could frustrate some users if they've partially completed the form then delete the image. Thanks for your comments. So then the best solution appears to be a decorator. On Oct 3, 2008, at 1:28 PM, Matthew Lurz wrote: In that case why not forego the remove action and the additional form logic and just display the form regardless of whether the image exists. When the user uploads a new image it would replace the existing one. Just wondering since it seems like the user would have to jump through an extra hoop to remove the image in order to upload a new one. The less a user does or has to do the better. Edward Haber wrote: Here's the solution I came up with. Thanks for all the input and help on this problem. This solution involves using a conditional statement in Zend_Form to create one of two element types. When the image is present the view helper decorator replaces the form element with the image and a link to a remove action. The view helper is below and will still be tweaked a bit but it should give the overall idea. This form class snippet has all the other elements removed from it for clarity class MyForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $db = Zend_Registry::get('db'); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -addPrefixPath('MF_Form_Element', 'MF/Form/Element/', 'element') -setAttrib('enctype', 'multipart/form-data') -setAttrib('id', 'article-form'); /** * image_name */ if ($options['image_id']) { $this-addElement('file', 'image_name', array( 'label' = 'Image', 'description' = 'To upload another photo delete this one first.', 'attribs' = array( 'imgSrc' = URL . '/media/articles/' . $options['image_id'] . '.jpg', 'action' = URL . '/admin/article/drop-image/' . $options['image_id'] ), 'decorators' = array( array('ViewHelper', array('helper' = 'mediaImage')), 'Errors', 'Description', array('HtmlTag', array('tag' = 'dd')), array('Label', array('tag' = 'dt')) ) )); } else { $this-addElement('file', 'image_name', array( 'label' = 'Upload an image', 'destination' = AP . '/application/tmp/upload-staging', 'required' = true, 'validators' = array( array('Size', '500Kb'), array('ImageSize', array(10, 10, 1024, 768)), array('Extension', array('jpg', 'jpeg')) ) )); } (other form elements removed) Then I use this helper: class MF_View_Helper_MediaImage { public function mediaImage($fieldname, $value = , $attribs = null) { $imgSrc = $attribs['imgSrc']; $action = $attribs['action']; $class = isset($attribs['class']) ? $attribs['class'] : 'image- remove-button'; return OUTPUT $imgSrc | $action Delete OUTPUT; } } This will pretty much be a reusable solution
[fw-general] Toggle file-upload element with uploaded image
I have a form that I want offer an image upload form element on. This has worked great using Zend_Form_Element_File. The upload works fine. What is the recommended way to have the uploaded image replace the upload form if an image exists? I don't need the solution coded, just the direction I should go. For example, I have the form as a class. SHould I pass the image name (if one exists) in the config array to the form? In the form i would then check for $config['imageName'] and if it exists display the image in place of the form element. This is another trick - how do display an image in a form? Is the answer toggling in the form class between the form element and an image? Or using CSS to display the image if it's there and hide the form (display:none for example). I'm just looking for best practice. I'm sure lots of people have already confronted this situation. Any input much appreciated! Thanks! Eddie
Re: [fw-general] Toggle file-upload element with uploaded image
Definitely view logic. Good call. Still I'm uncertain though because I'm working with an extended Zend_Form class. Perhaps I should make a custom Form Element? Or a custom Form Decorator? Or use the view helper decorator for just that element? I'd love to hear the Zend best practice take on this solution. Thanks! On Oct 2, 2008, at 10:50 PM, Matthew Lurz wrote: Sounds like view logic to me and so I would add a conditional in the view script. Edward Haber wrote: I have a form that I want offer an image upload form element on. This has worked great using Zend_Form_Element_File. The upload works fine. What is the recommended way to have the uploaded image replace the upload form if an image exists? I don't need the solution coded, just the direction I should go. For example, I have the form as a class. SHould I pass the image name (if one exists) in the config array to the form? In the form i would then check for $config['imageName'] and if it exists display the image in place of the form element. This is another trick - how do display an image in a form? Is the answer toggling in the form class between the form element and an image? Or using CSS to display the image if it's there and hide the form (display:none for example). I'm just looking for best practice. I'm sure lots of people have already confronted this situation. Any input much appreciated! Thanks! Eddie -- View this message in context: http://www.nabble.com/Toggle-file-upload-element-with-uploaded-image-tp19780259p19791263.html Sent from the Zend Framework mailing list archive at Nabble.com.
[fw-general] Zend_Form File element toggle between image
Thanks in advance for any thoughts on the following: 1) Can anyone suggest the best way to implement the following in a form: a Zend_Form_Element_File if no image has been uploaded. But if an image has been uploaded, the image appears with a delete button. Would a custom form element be best or would you just hide/show the various elements with Javascript? Or is it already built-in!? 2) And as a second question: I can't find any way to do this. Is there any way to just insert a random blob or row of HTML in your form like: dl dtLabel/dtddinput type.../dd dtLabel/dtddinput type.../dd dtLabel/dtddinput type.../dd pHi look at me!/p dtLabel/dtddinput type.../dd dtLabel/dtddinput type.../dd /dl Is there any way to insert the line Hi look at me! without decorating the element above it or below it and without creating a custom class? 3) Is there a problem with the current version of the Zend_Form_Element_File? The element doesn't appear to print with the row decorator. ?php class forms_AdminArticleForm extends Zend_Dojo_Form { public function __construct($options = null) { parent::__construct($options); $this-setName('article_form') -setAction(URL.'/admin/article/save') -setMethod('post') -setAttrib('enctype', 'multipart/form-data') -setAttrib('id', 'article-form'); $title = new Zend_Form_Element_Text('title'); $title-setLabel('Title') -setRequired(true) -addValidator('NotEmpty') -setAttribs(array('size'='40')) -addValidator('StringLength', false, array(1, 128)); $author = new Zend_Form_Element_Text('author'); $author-setLabel('Author') -setRequired(true) -setAttribs(array('size'='40')) -addValidator('NotEmpty') -addValidator('StringLength', false, array(1, 64)); $image = new Zend_Form_Element_File('image_name'); $image-setLabel('Upload an image:') -setDestination(AP . '/application/tmp/upload-staging') -addValidator('Size', '500kb'); $submit = new Zend_Form_Element_Submit('action'); $submit-setLabel('Save'); $this-addElements(array( $title, $author, $image, $submit )); } } This class generates the following HTML code: form id=article-form action=/admin/article/edit method=post enctype=multipart/form-data dl class=zend_form dtlabel for=title class=requiredTitle/label/dt ddinput type=text name=title id=title value=Here ya go size=40/dd dtlabel for=author class=requiredAuthor/label/dt ddinput type=text name=author id=author value=dudeness size=40/dd input type=file name=image_name id=image_name helper=formFile dtnbsp;/dt ddinput type=submit name=action id=action value=Save/dd dtnbsp;/dt ddinput type=hidden name=article_id value=4 id=article_id/ dd /dl /form
Re: [fw-general] View Helpers Repository
This is why I called the helper s instead of pluralize. If you'd like to contribute a better pluralization helper, email it to me or leave comments on the website. Maybe I wasn't clear. The point is not that I'm bringing to the Zend community my awesome personal view helpers, but rather I am trying to create a repository so that all ZF developers can share View Helpers as a resource. I'm just about to take the site down though seeing as this idea isn't of interest to anyone. Thanks! Eddie On Sep 25, 2008, at 2:31 PM, Matthew Ratzloff wrote: Regarding your plural (s) helper... person = people -Matt On Thu, Sep 25, 2008 at 10:53 AM, Edward Haber [EMAIL PROTECTED] wrote: I'm just in the first steps of putting together a ViewHelpers repository because there is such a serious need for a resources repository for ZF. I would welcome any thoughts on this implementation -- any intelligence on the idea itself (does it already exist in a better form somewhere else?) -- if this is planned by Zend in the future I would be happy not to waste my time. This is basically a code-repository Wiki: http://zfhelpers.com. If you want to email me your view helpers I'll add them to the site. LMK if you have any thoughts on better/easier administration. SO far I put up my view helpers I've used. Is this idea worthwhile? i'm curious if people want to trade view helpers and other resources. Thx! Eddie On Sep 25, 2008, at 1:47 PM, Jason Austin wrote: Count me in as a vote for doing this. It would be great to provide that type of resource to users. - Jason Matthew Weier O'Phinney wrote: -- mezoni [EMAIL PROTECTED] wrote (on Thursday, 25 September 2008, 09:13 AM -0700): Is ZF Teem planned official user plugin repository? Not currently; we've been kicking the idea around for a while, though. -- Jason Austin Senior Solutions Implementation Engineer NC State University - Office of Information Technology http://webapps.ncsu.edu 919.513-4372
[fw-general] View Helpers Repository
I'm just in the first steps of putting together a ViewHelpers repository because there is such a serious need for a resources repository for ZF. I would welcome any thoughts on this implementation -- any intelligence on the idea itself (does it already exist in a better form somewhere else?) -- if this is planned by Zend in the future I would be happy not to waste my time. This is basically a code-repository Wiki: http://zfhelpers.com. If you want to email me your view helpers I'll add them to the site. LMK if you have any thoughts on better/easier administration. SO far I put up my view helpers I've used. Is this idea worthwhile? i'm curious if people want to trade view helpers and other resources. Thx! Eddie On Sep 25, 2008, at 1:47 PM, Jason Austin wrote: Count me in as a vote for doing this. It would be great to provide that type of resource to users. - Jason Matthew Weier O'Phinney wrote: -- mezoni [EMAIL PROTECTED] wrote (on Thursday, 25 September 2008, 09:13 AM -0700): Is ZF Teem planned official user plugin repository? Not currently; we've been kicking the idea around for a while, though. -- Jason Austin Senior Solutions Implementation Engineer NC State University - Office of Information Technology http://webapps.ncsu.edu 919.513-4372
Re: [fw-general] multiple models with same name (m1/models/pages.php and m2/models/pages.php)
First of all, had I known you were a Zend engineer I probably wouldn't have opened my big mouth. I'm new to the list. :-) But since it's already open, i'll just offer a few points of view from my perspective. On Sep 22, 2008, at 12:55 PM, Bill Karwin wrote: Edward Haber wrote: Another problem with the proposed solution is that it only makes one module's models path active at a time. I think the ideal when working with multiple modules is to have the option of using model code from more than one module models directory at a time, especially in cases of interdependency. No, that's not ideal, that's actually what I was recommending to _prevent_. I see your point that having all the models directories of all modules in the include path leads to namespace collision in a modular framework like Joomla. I was under the impression you were making a modular application where the code is grouped by functionality rather than a third-party-capable modular framework. A module has its own controllers and views, and should have its own set of models as well. One module should not be allowed to reference another module's controllers, views, or models. If you follow this rule, then it solves all the problems of ambiguity and collision. In my opinion, if you have three modules implementing a Pages model that has a duplicate fetchMyPages() method (say with differing parameters per module) then it seems anti-DRY to repeat the same file/ class/method three times (then keep them sync'd when making bug fixes, then write three sets of test classes, etc.). It comes down to (for me) if the class in question is used in the exact same context in multiple modules. If it's three unrelated types of pages that has one solution. If it's three versions of basically the same class and methods then it seems anti-DRY to me. Don't separate your application into modules if the modules aren't separate. If you want to share model classes among all your controllers, then don't create modules. In my case, It was a lot easier to manage the front-end (controllers and views) within modules but just use a single model directory. This particular organizational method helped the front-end developers find code more easily and allowed for teams to work more independently. These solutions are unique to the needs of each application. If it best suited your application, there's no reason you couldn't have fifty front-ends (of controllers/views) connected to one set of models. It also suited the application well, which has three front-end areas (which I made modules) where users of one area never are users in another area. I agree I would approach a Drupal/Joomla-like architecture differently, but this architecture is clear, obvious, and breaks no MVC conventions. If however it is a must that you remain encapsulation and that these modules are dropped into other apps and need to work this way, then I would change the names of your models entirely. This doesn't work in general, if you drop third-party modules into your app, or drop your module into someone else's app. Only if you have control over the class naming of all modules in an app does your recommendation work. If you're the author of all the modules in the app, then why are you using modules at all? The purpose of modules is to provide isolation between sets of classes. Many people seem to use modules to create friendly URL's like /myapp/admin/controller/action but you can do this by creating custom router rules. You don't have to use modules simply to define URL's. This seems unnecessarily strict. In my opinion, modules can be *more* than this or *less* than this depending on the requirements of the application. Part of the beauty of the Zend Framework (and PHP) is that it is so flexible to different architecture requirements. I agree it's really not that hard to write a route. I'm using plenty of them in the app. But why not, if it suits the grouping of your code, and makes the urls work, take advantage of built-in features of the framework? Thanks for your notes. I hope to learn a lot from the members of the list. Eddie Regards, Bill Karwin -- View this message in context: http://www.nabble.com/multiple-models-with-same-name-%28m1-models-pages.php-and-m2-models-pages.php%29-tp19607614p19612115.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] multiple models with same name (m1/models/pages.php and m2/models/pages.php)
I've read about this issue before. I'm no ZF author, but my $.02 is, even though this helps encapsulate your code into modules more precisely, is that it's not good in practice. For one thing it creates files with the same name that might be confusing. It also has multiple versions of the same model, only one of which may be instantiated at a time. My thought is if you run into this problem (as I just did on my current project), then the application architecture requires a different approach. On my current project I decided it made more sense to move all my models out of app/modules/module-name/models directories and into app/models than to strictly enforce module encapsulation. If however it is a must that you remain encapsulation and that these modules are dropped into other apps and need to work this way, then I would change the names of your models entirely. The Pages models could then be named AdminPages and UserPages (avoiding the same model name). This would imply the different model classes behave and act differently (and may not require one another). Good luck, Eddie --- Eddie Haber Engineer + Architect MetaFoundry, LLC Email: [EMAIL PROTECTED] Direct: (917) 865-0494 IM: eddiehaber URL: http://www.metafoundry.com --- On Sep 22, 2008, at 9:08 AM, chrisweb wrote: Hello, I have a noob questions for you ;-) ... i did not know if its better to put this question in the core or perhaps zend_db folder its why i published it here ... i also searched a lot in the mailinglist archive but did not find a similar question, sry if its an duplicate :blush: my website structure is this one: website - application -- default --- controllers IndexController.php --- layout --- models Pages.php --- views scripts - index -- index.phtml -- admin --- controllers IndexController.php --- layout --- models Apages.php --- views scripts - index -- index.phtml - library - public As you can see it, i have two similar models, the first model is called Pages.php, the second one has the same name Pages.php In the beginning i had the following code in Default controllers IndexController.php: Zend_Loader::loadClass('Pages', '../application/default/models/'); ... and the following code in Admin controllers IndexController.php: Zend_Loader::loadClass('Pages', '../application/admin/models/'); ... everything worked fine, but then i wanted to try to use the autoloader for classes and files its why i changed my bootstrap file, now its like this (with autoloader): ?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 1); // setup directories paths set_include_path( get_include_path() . PATH_SEPARATOR . '../library' . PATH_SEPARATOR . '../application/default/models' . PATH_SEPARATOR . '../application/admin/models' ); require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload(); // Create registry object and setting it as the static instance in the Zend_Registry class $registry = new Zend_Registry(); Zend_Registry::setInstance($registry); $dbconfig = new Zend_Config_Ini('../application/config.ini', 'database'); $registry-set('dbconfig', $dbconfig); // setup database $db = Zend_Db::factory($dbconfig-db); Zend_Db_Table::setDefaultAdapter($db); $db-setFetchMode(Zend_Db::FETCH_OBJ); $db-query('SET NAMES utf8'); Zend_Registry::set('db',$db); // setup controller $frontController = Zend_Controller_Front::getInstance(); $registry-set('front', $frontController); $frontController-throwExceptions(false); $frontController-setControllerDirectory(array( 'default' = '../application/default/controllers', 'admin' = '../application/admin/controllers' )); $frontController-setParam('useDefaultControllerAlways', true); $frontController-setRequest(new Zend_Controller_Request_Http()); // setup views $view = new Zend_View(); Zend_Registry::set('View',$view); // setup Zend Layout Zend_Layout::startMvc(); // run! $frontController-dispatch(); // dispatche! In default models Pages.php i have the following code: class Pages extends Zend_Db_Table { protected $_name = 'pages'; // table name protected $_primary = 'id'; // table primary key protected $_sequence = true; // if auto increment is on public function outputpageslist() { $pagesTable = new Pages(); $query = SELECT id, title FROM pages WHERE actif='1' AND parent='0' ORDER BY position; $db = $pagesTable-getAdapter(); $result = $db-query($query); return $result-fetchAll(); } } In admin models Pages.php i have the following code: class Pages extends Zend_Db_Table { protected $_name = 'pages'; // table name protected $_primary = 'id'; // table primary key protected $_sequence = true; // if auto increment is on public function getpageslist() { $pagesTable = new Pages(); $query = SELECT id,
Re: [fw-general] multiple models with same name (m1/models/pages.php and m2/models/pages.php)
I think Bill Karwin makes a fine point and I don't know if my thoughts here are correct form academically speaking (and I'm curious to get a definitive word on this). I've read about this issue before. I'm no ZF author, but my $.02 is, even though this helps encapsulate your code into modules more precisely, is that it's not good in practice. For one thing it creates files with the same name that might be confusing. It also has multiple versions of the same model, only one of which may be instantiated at a time. My thought is if you run into this problem (as I just did on my current project), then the application architecture requires a different approach since an app with four Pages models seems problematic to me from an architecture viewpoint. On my current project I decided it made more sense to move all my models out of app/modules/module-name/models directories and into app/models than to strictly enforce module encapsulation. Another problem with the proposed solution is that it only makes one module's models path active at a time. I think the ideal when working with multiple modules is to have the option of using model code from more than one module models directory at a time, especially in cases of interdependency. If however it is a must that you remain encapsulation and that these modules are dropped into other apps and need to work this way, then I would change the names of your models entirely. The Pages models could then be named AdminPages and UserPages (avoiding the same model name). This would imply the different model classes behave and act differently (and may not require one another). Good luck, Eddie --- Eddie Haber Engineer + Architect MetaFoundry, LLC Email: [EMAIL PROTECTED] Direct: (917) 865-0494 IM: eddiehaber URL: http://www.metafoundry.com --- On Sep 22, 2008, at 9:08 AM, chrisweb wrote: Hello, I have a noob questions for you ;-) ... i did not know if its better to put this question in the core or perhaps zend_db folder its why i published it here ... i also searched a lot in the mailinglist archive but did not find a similar question, sry if its an duplicate :blush: my website structure is this one: website - application -- default --- controllers IndexController.php --- layout --- models Pages.php --- views scripts - index -- index.phtml -- admin --- controllers IndexController.php --- layout --- models Apages.php --- views scripts - index -- index.phtml - library - public As you can see it, i have two similar models, the first model is called Pages.php, the second one has the same name Pages.php In the beginning i had the following code in Default controllers IndexController.php: Zend_Loader::loadClass('Pages', '../application/default/models/'); ... and the following code in Admin controllers IndexController.php: Zend_Loader::loadClass('Pages', '../application/admin/models/'); ... everything worked fine, but then i wanted to try to use the autoloader for classes and files its why i changed my bootstrap file, now its like this (with autoloader): ?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 1); // setup directories paths set_include_path( get_include_path() . PATH_SEPARATOR . '../library' . PATH_SEPARATOR . '../application/default/models' . PATH_SEPARATOR . '../application/admin/models' ); require_once 'Zend/Loader.php'; Zend_Loader::registerAutoload(); // Create registry object and setting it as the static instance in the Zend_Registry class $registry = new Zend_Registry(); Zend_Registry::setInstance($registry); $dbconfig = new Zend_Config_Ini('../application/config.ini', 'database'); $registry-set('dbconfig', $dbconfig); // setup database $db = Zend_Db::factory($dbconfig-db); Zend_Db_Table::setDefaultAdapter($db); $db-setFetchMode(Zend_Db::FETCH_OBJ); $db-query('SET NAMES utf8'); Zend_Registry::set('db',$db); // setup controller $frontController = Zend_Controller_Front::getInstance(); $registry-set('front', $frontController); $frontController-throwExceptions(false); $frontController-setControllerDirectory(array( 'default' = '../application/default/controllers', 'admin' = '../application/admin/controllers' )); $frontController-setParam('useDefaultControllerAlways', true); $frontController-setRequest(new Zend_Controller_Request_Http()); // setup views $view = new Zend_View(); Zend_Registry::set('View',$view); // setup Zend Layout Zend_Layout::startMvc(); // run! $frontController-dispatch(); // dispatche! In default models Pages.php i have the following code: class Pages extends Zend_Db_Table { protected $_name = 'pages'; // table name protected $_primary = 'id'; // table primary key protected $_sequence = true; // if auto increment is on public function outputpageslist() { $pagesTable = new Pages(); $query = SELECT id, title
[fw-general] Sender CC'd post?
As someone who fell for this myself (sending duplicate emails to the list), I think the sender of a post should receive a copy. Eddie