This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » forward404Unless() problem
forward404Unless() problem [message #102588] Fri, 16 July 2010 13:41 Go to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

Hi,

i am trying to redirect by action when it returns an empty result set using foward404Unless(). the problem is it doesnt seem to have any effect. It just brings me a page without the content

in my action i have this
public function executeIndex(sfWebRequest $request) {

        $this->articles = Doctrine_Core::getTable('Articles')->getArticles($request->getParameter('id'));

        $this->forward404Unless($this->articles);
    }


this method is retrieved from the table as follows

 public function getArticle($id) {

        $q = $this->createQuery('a')
                        ->where('a.id =?', $id);

        return $q->execute();
    }



anyone knows how to fix this?

Thanks
Re: forward404Unless() problem [message #102591 is a reply to message #102588 ] Fri, 16 July 2010 14:14 Go to previous messageGo to next message
esukf  is currently offline esukf
Messages: 65
Registered: December 2006
Member
The method you are calling is not the one you showed, getArticles() and getArticle() without s. Is that just a typing mistake when you wrote your post?
Re: forward404Unless() problem [message #102593 is a reply to message #102591 ] Fri, 16 July 2010 14:20 Go to previous messageGo to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

sorry. this is the actual method

public function getArticles($id) {

        $q = $this->createQuery('a')
                        ->where('a.category_id =?', $id)
                        ->orderBy('a.id')
                        ->limit('1');

        return $q->execute();
    }



i have a few records in the database that call this method and data is displayed. but where there is no record, the forward404Unless() method does not redirect

[Updated on: Fri, 16 July 2010 14:28]

Re: forward404Unless() problem [message #102615 is a reply to message #102588 ] Fri, 16 July 2010 15:53 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi,

maybe, even if there is no record, $q->execute() returns an empty array or something like that which is not a compliant condition to forward4040Unless() function ...

What is the result of a var_dump($this->articles) (in your action) when no record is returned ?
Re: forward404Unless() problem [message #102619 is a reply to message #102615 ] Fri, 16 July 2010 16:13 Go to previous messageGo to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

hi, i suspected that...var_dump returns a big bunch of array and hungs the browser. If forward404Unless() cannot do the trick..what can? Or is there another way to format my queries?
Re: forward404Unless() problem [message #102623 is a reply to message #102588 ] Fri, 16 July 2010 16:45 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi,

I am not an expert in Doctrine (I didn't make any project with it yet) ... but, I have maybe an answer for you Very Happy
If I am not wrong, Doctrine has an integrated method to do what you want :

Doctrine::getTable('Articles')->find($request->getParameter('id'));


FInd here : http://www.symfony-project.org/jobeet/1_4/Doctrine/en/04 just before the Slots chapter

Maybe it will help you Very Happy

[Updated on: Fri, 16 July 2010 16:47]

Re: forward404Unless() problem [message #102630 is a reply to message #102623 ] Fri, 16 July 2010 19:34 Go to previous messageGo to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

sescandell wrote on Fri, 16 July 2010 17:45

Hi,

I am not an expert in Doctrine (I didn't make any project with it yet) ... but, I have maybe an answer for you Very Happy
If I am not wrong, Doctrine has an integrated method to do what you want :

Doctrine::getTable('Articles')->find($request->getParameter('id'));


FInd here : http://www.symfony-project.org/jobeet/1_4/Doctrine/en/04 just before the Slots chapter

Maybe it will help you Very Happy



Hi again, i know about that method. It didn't work though. My worry is about customized queries from Tables.
Re: forward404Unless() problem [message #102667 is a reply to message #102630 ] Sat, 17 July 2010 18:55 Go to previous messageGo to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

anyone got any ideas for my problem Smile
thanks
Re: forward404Unless() problem [message #102672 is a reply to message #102588 ] Sat, 17 July 2010 19:32 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi,

Ok, so I made some test using the simple following schema :
Articles:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }


the following action
public function executeIndex(sfWebRequest $request)
  {
    $this->article = ArticlesTable::getInstance->getArticle($request->getParameter('id'));

    $this->forward404Unless($this->article);
  }


and the following model
class ArticlesTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('Articles');
    }
    
    public function getArticle($id)
    {
        $q = $this->createQuery('a')
                        ->where('a.id =?', $id)
                        ->orderBy('a.id')
                        ->limit(1);

        return $q->execute();
    }
}


In fact ... execute() returns an object in all cases. So I see two things:
1- because your method limit to one the count of article retrive from the method getArticles (by the way ... why is there a 'S' at this method ? should it be getArticle($id) ?), you can modify it by the following:
public function getArticle($id)
    {
        $q = $this->createQuery('a')
                        ->where('a.id =?', $id)
                        ->orderBy('a.id');

        return $q->fetchOne();
    }

and do not change your action

2- if for any reason, the first solution doesn't satisfy you, you can simply change your action by this:
public function executeIndex(sfWebRequest $request)
  {
    $this->article = Doctrine_Core::getTable('Articles')->getArticles($request->getParameter('id'));

    $this->forward404Unless($this->articles->count());
  }


In fact, the tricky thing is that, even if execute() get an object or not, it returns an non-null object (object from class Doctrine_Collection) which is a compliant condition to forward404Unless and so do not forward your request ...
So with this new action, we check that this object is not empty.

Hoping that I helped you Wink


PS : Have a look to the following files to understand how it is working:
lib\vendor\symfony1.4\lib\plugins\sfDoctrinePlugin\lib\vendo r\doctrine\Doctrine\Table.php => line 1028
lib\vendor\symfony1.4\lib\plugins\sfDoctrinePlugin\lib\vendo r\doctrine\Doctrine\Query.php => line 59
lib\vendor\symfony1.4\lib\plugins\sfDoctrinePlugin\lib\vendo r\doctrine\Doctrine\Query\Abstract.php => line 990
lib\vendor\symfony1.4\lib\plugins\sfDoctrinePlugin\lib\vendo r\doctrine\Doctrine\Collection.php

[Updated on: Sat, 17 July 2010 19:56]

Re: forward404Unless() problem [message #102675 is a reply to message #102672 ] Sat, 17 July 2010 20:53 Go to previous messageGo to next message
el-sid  is currently offline el-sid
Messages: 64
Registered: September 2009
Location: Nairobi, Kenya
Member

@sescandell...dude!! you are awesome!! Though to be honest, i still don't quite get how you explained it.

by using var_dump, realized $q->execute() returns an array whether the result set is empty or not. Doesn't fetchOne() also return an array?
Re: forward404Unless() problem [message #102683 is a reply to message #102588 ] Sun, 18 July 2010 02:54 Go to previous message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi !

Quote:

Though to be honest, i still don't quite get how you explained it.

In fact ... it doesn't really matter Laughing

Quote:

by using var_dump, realized $q->execute() returns an array whether the result set is empty or not. Doesn't fetchOne() also return an array?


No, in fact $q->execute() doesn't return an array. It returns an Object of Doctrine_Collection class (which is, vulgaraly, a container of an array and so much more). So when you call the method execute(), it creates a Doctrine_Collection and fills its internal array with the request result. So, if there is some results, the internal array will not be empty and everything is going to work. But if there is no result. Indeed, the internal array will be empty, but the object itself is existing ... I mean $q->execute() returns you an object of Doctrine_Collection (with an empty array) but the object itself exists ... that's why forward404Unless doesn't succeed ...

About fetchOne() it is just a little bit different. In fact fetchOne() use internaly the execute() method and then process its result. For this one, it is in the file lib\vendor\symfony1.4\lib\plugins\sfDoctrinePlugin\lib\vendo r\doctrine\Doctrine\Query.php on line 279. In fact, if there is a result, fetchOne() returns an object of Doctrine_Record class, or an array. But if there is no result, fetchOne() simply returns FALSE ... and not an array neither an object.

Quote:

dude!! you are awesome!!

Very Happy ... thanks but ... I am not so much ... Very Happy ... I am just trying to understand how is Symfony working and, thanks to that, you can sometimes understand why it is reacting like that ...
If I can give you an advice (it is like that I proceed for you issue). When I have a problem, I am checking a simple var_dump() result or an echo on my objects (deleting all personnal __toString() method). Then, like in that case, if var_dump() doesn't mean anything (or something very huge) it probably means that there is a specific object beside. So, I take my code, look what I am doing and especialy the objects used. In that case, I focused me on the $q variable. We create it with the createQuery() method. So I looked for what this method really returns (and so you will see that it returns a Doctrine_Query object). Then I looked for the execute() method and so on ...


Have fun Wink

[Updated on: Sun, 18 July 2010 02:59]

Previous Topic:Error detected in sfValidatorDate
Next Topic:admin generator language
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software