Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP AutoComplete Question
    primarykey
    data
    text
    <p>I am working on a book review application and I am using autoComplete to search for titles in when creating a review. The review model has an associated book_id field and the relationship is setup as the review hasOne book and a book hasMany reviews.</p> <p>I am trying to pass the Book.id (into the book_id field), but I want to display Book.name for the user to select from. With the default setup (accomplished via CakePHP's tutorial), I can only pass Book.name. Is it possible to display the name and pass the id?</p> <p>Also, I am passing it via the following code in the create() action of the review controller:</p> <pre><code>$this-&gt;data['Review']['book_id'] = $this-&gt;data['Book']['id']; </code></pre> <p>Is that the proper way to do it in CakePHP? I know in Ruby on Rails, it is automatic, but I can't seem to make it work automagically in CakePHP. Finally, I am not using the generator because it is not available in my shared hosting environment... so if this is the wrong way, what do I need other than associates in my models to make it happen automatically?</p> <p>Thanks for the help and I promise this is my question for awhile...</p> <p>UPDATE- I tried the following, but it is not working. Any ideas why?</p> <pre><code> function autoComplete() { $this-&gt;set('books', $this-&gt;Book-&gt;find('all', array( 'conditions' =&gt; array( 'Book.name LIKE' =&gt; $this-&gt;data['Book']['name'].'%' ), 'fields' =&gt; array('id','name') ))); $this-&gt;layout = 'ajax'; } </code></pre> <p>The problem is that when I use the code above in the controller, the form submits, but it doesn't save the record... No errors are also thrown, which is weird.</p> <p>UPDATE2:</p> <p>I have determine that the reason this isn't working is because the array types are different and you can't change the array type with the autoComplete helper. As a workaround, I tried the follow, but it isn't working. Can anyone offer guidance why?</p> <pre><code>function create() { if($this-&gt;Review-&gt;create($this-&gt;data) &amp;&amp; $this-&gt;Review-&gt;validates()) { $this-&gt;data['Review']['user_id'] = $this-&gt;Session-&gt;read('Auth.User.id'); $this-&gt;Book-&gt;find('first', array('fields' =&gt; array('Book.id'), 'conditions' =&gt; array('Book.name' =&gt; $this-&gt;data['Book']['name']))); $this-&gt;data['Review']['book_id'] = $this-&gt;Book-&gt;id; $this-&gt;Review-&gt;save($this-&gt;data); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $errors = $this-&gt;Review-&gt;invalidFields(); } } </code></pre> <p>FINAL UPDATE:</p> <p>Ok, I found that the helper only takes the find(all) type or array and that the "id" field wasn't passing because it only applied to the autoComplete's LI list being generated. So, I used the observeField to obtain the information and then do a database lookup and tried to create a hidden field on the fly with the ID, but that didn't work. Finally, the observeField would only take the characters that I put in instead of what I clicked, due to an apparent Scriptaculous limitation. So, I ended up going to a dropdown box solution for now and may eventually look into something else. Thanks for all of the help anyway!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload