Re: Model help
Jeremy's advice is sound :o) Just thought I would add a very simple, but useful note. If you have got to the point where you have pulled some data from the system and don't know what to do with it then you probably need to familiarise yourself with how Cake returns that data to you. So in your view type ?php echo debug($items); ? This will echo out your multi-dimensional data array showing you exactly what data result you got and giving you vital clues as to how you need to loop through your data array to show your index or view. If you are wanting to present your list of items in a paginated list/ table then you may also want to have a look at pagination too. Once you get your head around it you'll find you instantly paginate all indexes and have quick access to easy pagination and sorting links. http://book.cakephp.org/view/164/Pagination HTH Paul Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en To unsubscribe from this group, send email to cake-php+unsubscribegooglegroups.com or reply to this email with the words REMOVE ME as the subject.
Re: Model help
The secret lies in setting up your models correctly. You also will find life easier if you use the field name 'name' instead of 'label'. Doing this means that you can easily create drop downs populated with 'id' and 'name' where 'name' is the displayed value. This is the default Cake convention. I'd say your models ought to look something like this: Item: var $belongsTo = array( 'City' = array( 'className' = 'City', 'foreignKey' = 'city_id' ), 'Category' = array( 'className' = 'Category', 'foreignKey' = 'category_id' ) ); City: var $hasMany = array( 'Item' = array( 'className' = 'Item', 'foreignKey' = 'city_id' ) ); Category: var $hasMany = array( 'Item' = array( 'className' = 'Item', 'foreignKey' = 'category_id' ) ); This set ups the joins between your tables. I'd also strongly recommend using recursion and the containable behaviour as it gives you tremendous control over exactly what gets returned. I wouldn't leave home without it now, and place it in my AppModel: var $actsAs = array('Containable'); var $recursive = -1; Then your SQL statement (when called from within the items controller) becomes a find: $variable = $this-Item-find( 'all', array( 'fields' = array( 'City.id', 'City.name' ), 'contain' = array( 'Category' = array( 'Category.label' = 'Doctors' // mostly going to be a variable, e.g. $categoryLabel ), 'City' = array( 'City.label' = 'Boston' // mostly going to be a variable, e.g. $cityLabel ) ) ) ); I haven't tested that - just typed it here - but it ought to work. Jeremy Burns jeremybu...@me.com On 18 Mar 2010, at 01:27, Andrew wrote: Hello, Thank you for your time, I am a new user of CakePHP. I recently found CakePHP and am still trying to get used to the MVC architecture. (By the way, it's brilliant). However I'm still getting used to reformatting my SQL into cakephp. My question is as follows. I have an SQL statement that (simplified) is like the following: select i.name from item i, city c, category ca where ca.id = i.category_id and c.id = i.city_id and ca.label = Doctors and c.label = Boston I have been able to create step one -- $this-set('items', $this-Item-findAllByCategoryId('1')); And now I'm totally stuck, as this is the first model I've created. I had a look through the model documentation but couldn't see anywhere that dealt with SQL from two tables :-( Thanks Andrew Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: Model Help
Right after I posted this I re-read the Model section, and found the way to solve the problem. $this-Item-ItemList-save($this-data); $itemListId = $this-Item-ItemList-getLastInsertId(); $this-data['Item']['item_list_id'] = $itemListId; This both creates the new list and associates it to the new item. -John On Jul 15, 1:02 am, jmartyniak [EMAIL PROTECTED] wrote: I have a model called Item (model, views, controller) in my add method reached by localhost/item/add, I would like to give the user the ability to create a new ItemList db record from the Item add form. I currently am using the following form code (add.thtml): labelList:/label ?php echo $html-inputTag('ItemList/name', array('size' = '64')) ? ?php echo $html-tagErrorMsg('ItemList/name', 'List must be less than 64 characters') ? The following code in the add method of the ItemController: function add() { if (!empty($this-data)) { if ($this-Item-validates($this-data)) { $this-Item-save($this-data); $this-redirect('/item/index'); } } else { $this-validateErrors($this-Item); } } It doesn't save the ItemList object, I have also tried trying to save the ItemList explicitly using: $this-Item-save($this-data['ItemList']); If I change it to the following it generates and error, that ItemList is null: $this-ItemList-save($this-data); and $this-ItemList-save($this-data['ItemList']); Nothing works. I am new to Cake, so still working through the fundamentals. Any help would be greatly appreciated. -John --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---