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/partners lists partners fine. Performing a
search posts to http://mydomain.com/partners/search and shows 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.json gives the 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 <p...@webbedit.co.uk> 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 <stevefis...@gmail.com> 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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to