Steve

Don't worry I am a self-taught PHP hack and learned the hard way, but
I must admit you're adventurous for tackling a RESTful app whilst at
the bottom of your PHP and CakePHP learning curve.

When it comes to passing variables/arrays from the controller to your
view you can name them whatever you want, there are no conventions at
that point as long as you use the same array name in your view as you
set in the controller.

If you're saying your controller action is stopping at

function search() {
  if(empty($this->data)) {
    $this->render();

Then you're not passing your search term from a field with a name of
data[Partner][q].  Cake will only place form data in the $this->data
array if you post fields with a name format of data[ModelName]
[field_name];  You can see if your search term is elsewhere in the
request params by using echo debug($this->params);

Past this point your code does the following:

$partners=$this->Partner->find('all', array('conditions' =>
$conditions));
// sets a $partners array in the controller

$this->set('partners', $this->paginate());
// sets a paginated $partners array in the view

$this->set('partners', $partners);
// overwrites the view's paginated $partners array with the array you
set in the controller previously

So with this code you should have a $partners array in the view
containing the results of:
$this->Partner->find('all', array('conditions' => $conditions));

What do you get if you echo debug($partners); in the view?

HTH, Paul.
@phpMagpie

On Sep 16, 7:12 pm, steveo <[email protected]> wrote:
> Paul,
>
> Adding some more information - I just found the first part of the
> problem from the stack trace. When I invoke the /partners/search.json
> URL, the search() function is not getting any data, so it just runs to
> the first part of the IF statement and performs $this->render();
> (which is why I get the undefined variable: partner (or partners)!!
> So, I guess my question now is how to get the search result data to
> output in JSON!!??
>
> function search() {
>             if(empty($this->data)) {
>                 $this->render();
>             } else {
>
>                 $search_term=$this->data['Partner']['q'];
>                 $conditions = array("partner_name LIKE" => "%
> {$search_term}%");
>                 $partners=$this->Partner->find('all',
> array('conditions' => $conditions));
>                 //var_dump($partners);
>                 $this->Partner->recursive = -1;
>                 $this->set('partners', $this->paginate());
>                 $this->set('partners', $partners);
>                 //$this->render("index");
>             }
>         }
>
> On Sep 16, 1:48 pm, steveo <[email protected]> wrote:
>
>
>
>
>
>
>
> > Paul,
>
> > First off, I'm *very* new to CakePHP and not much of a PHP programmer
> > (more of a hack than anything!). I'm still grasping the whole MVC
> > architecture paradigm, so the plural/singular thing is still settling
> > in.
>
> > Basically, I created a CRUD interface from the CakePHP console to a
> > MySQL database, which worked perfectly! I then tried implementing the
> > CakeDC search plugin, but was completely frustrated with it and could
> > never get it to function (real-life examples are hard to find on it
> > and the blog example just didn't have enough detail for customizing
> > for my application).
>
> > Anyhow, the main part of the app consists of a partners controller 
> > -http://mydomain.com/partners-which basically lists all partners in a
> > paginated view (there are about 3000 partners). I wanted a search
> > interface because paging through 3000 records to find the one you want
> > to edit would just not be possible.
>
> > I implemented the search, as described earlier in this thread, and it
> > has worked fine (although probably not implemented completely
> > cleanly). I wanted to just have the search post back to itself, which
> > I couldn't figure out, so I created a search template which is pretty
> > much a copy of the partners template.
>
> > So,http://mydomain.com/partnerslistspartners fine. Performing a
> > search posts tohttp://mydomain.com/partners/searchandshows an
> > appropriate search result. All good.
>
> > I need to implement ReST now and have some endpoints which return JSON
> > (or XML) formatted results. Continuing on...
>
> >http://mydomain.com/partners.json(and/partners.xml) both output
> > expected results (although it is quite meaningless since it is just
> > the first 10 records). My index.ctp for the JSON output has just one
> > line of code: <?php echo json_encode($partners); ?> (notice the
> > plural, which works).
>
> > My dilemma now is the search output:
>
> >http://mydomain.com/search.jsongivesthe error Undefined variable:
> > partners (/views\/partners\/json\/search.ctp). The line of code in
> > this template is: <?php echo json_encode($partners); ?> (which works
> > just fine for /partners.json). If I change it to <?php echo
> > json_encode($partner); ?> (singular) is get the same error message,
> > just Undefined variable: partner (now singular). So as you can see
> > regardless of singular/plural in the search JSON output, I still get
> > an error!
>
> > <steve>
>
> > On Sep 16, 5:22 am, WebbedIT <[email protected]> wrote:
>
> > > Take JSON out of the equation and simply check to see if the 
> > > model->find() call is returning anything
>
> > > echo debug($this->paginate());
>
> > > Although from your post above it seems the problem is that you passing
> > > partners (plural) to the view and echoing partner (singlular) in the
> > > view.
>
> > > Why are you linking what happens with index and view to what happens
> > > in search?  They should all be different actions with different views?
>
> > > HTH, Paul
> > > @phpMagpie
>
> > > P.S. I can highly reccomend CakeDC's search plugin
>
> > > On Sep 15, 2:51 pm, steveo <[email protected]> wrote:
>
> > > > Hey folks! I'm fairly new to CakePHP, but so far I love it! I had some
> > > > troubles getting a search interface to work against my database, but
> > > > after googling around I was finally able to put the pieces together
> > > > and get it to work. I am not in the process of enabling REST
> > > > interfaces and outputting in JSON/XML. I have everything working just
> > > > fine in my index and view templates (where I can just add .json
> > > > or .xml to the end of the URL and get the correct output. However, I
> > > > am not able to get JSON/XML output from a search! It looks like my
> > > > array is empty and I'm not sure how to pass the data over.
>
> > > > I have the following code in my main controller:
>
> > > > function search() {
> > > >             if(empty($this->data)) {
> > > >                 $this->render();
> > > >             } else {
>
> > > >                 $search_term=$this->data['Partner']['q'];
> > > >                 $conditions = array("partner_name LIKE" => "%
> > > > {$search_term}%");
> > > >                 $partners=$this->Partner->find('all',
> > > > array('conditions' => $conditions));
> > > >                 //var_dump($partners);
> > > >                 $this->Partner->recursive = -1;
> > > >                 $this->set('partners', $this->paginate());
> > > >                 $this->set('partners', $partners);
> > > >                 $this->render("index");
> > > >             }
> > > >         }
>
> > > > Like I said, it works perfectly fine for the index and view, but not
> > > > for search. Instead, I get the following error:
>
> > > > Undefined variable: partner [APP/views/layouts/json/default.ctp, line
> > > > 1]null
>
> > > > Now, the tricky part. The json/default.ctp works for index and view,
> > > > so if I change the variable to "partners", it will no longer work for
> > > > index and view (in addition, changing to "partners" will not work for
> > > > the json/xml output either). Since I am seeing "null" in the error
> > > > message, I'm assuming that the partners variable is not getting passed
> > > > to the search (or default) template. Anyone think they can help me?
> > > > Thanks!!!

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to