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 » legacy support » symfony 1.1 » Problem with data selection
Problem with data selection [message #63927] Sun, 26 October 2008 04:04 Go to next message
zonyzeb  is currently offline zonyzeb
Messages: 12
Registered: October 2008
Junior Member
Hello,

I am using Symfony1.1 and when ever I try to include a criteria in my query it is showing error.

My code is..

public function executeList($request)
{
if(isset($_SESSION['logged'])){
if( ($_SESSION['role'] == 'admin') && $_SESSION['privilege'] == 'all'))
{
$criteria = new Criteria();
$criteria = add(UsersPeer::USERNAME,'abc');
$this->users = UsersPeer::doSelect($criteria);
sfView::SUCCESS;
}
}
}

And error I am getting is....

Fatal error: Call to undefined function add() in /../action.class.php


Thanks,
Sony Sebastian
Re: Problem with data selection [message #63928 is a reply to message #63927 ] Sun, 26 October 2008 08:36 Go to previous messageGo to next message
upalom00  is currently offline upalom00
Messages: 19
Registered: October 2008
Location: California
Junior Member

Hi

$criteria is an obj.

Thus - you should try doing this insead:

$criteria = new Criteria();
$criteria->add(UsersPeer::USERNAME,'abc');

Re: Problem with data selection [message #63929 is a reply to message #63928 ] Sun, 26 October 2008 09:48 Go to previous messageGo to next message
zonyzeb  is currently offline zonyzeb
Messages: 12
Registered: October 2008
Junior Member
Thanks for your reply.
The issue is now fixed....

And Now I have another issue, I can't join two tables using addJoin().

my code is ..
public function execute($request){
$c =new Criteria();
$c->addJoin(UsersPeer::ROLEID,RolesPeer::ROLEID);
$c->add(UsersPeer::USERID,$userId);
$this->users = UsersPeer::doSelect($c);
}

And I am getting the output from the first table(Users) only.


Thanks,
Sony Sebastian
Re: Problem with data selection [message #63931 is a reply to message #63927 ] Sun, 26 October 2008 10:03 Go to previous messageGo to next message
cokker  is currently offline cokker
Messages: 582
Registered: January 2007
Location: Germany
Faithful Member
You get an array of User objects containing the role.

iterate over $users and use the function getRoles()on each object;

greets
sven
Re: Problem with data selection [message #63932 is a reply to message #63929 ] Sun, 26 October 2008 10:03 Go to previous messageGo to next message
upalom00  is currently offline upalom00
Messages: 19
Registered: October 2008
Location: California
Junior Member
Try adding in what columns you want to see in you output.

public function execute($request){
$c =new Criteria();
$c->addJoin(UsersPeer::ROLEID,RolesPeer::ROLEID);
$c->add(UsersPeer::USERID,$userId);

//---------
$c->clearSelectColumns();
$c->addSelectColumn(RolesPeer::ROLEID);
//---------

$this->users = UsersPeer::doSelect($c);
}
Re: Problem with data selection [message #63933 is a reply to message #63931 ] Sun, 26 October 2008 10:04 Go to previous messageGo to next message
upalom00  is currently offline upalom00
Messages: 19
Registered: October 2008
Location: California
Junior Member
cool@sven

[Updated on: Sun, 26 October 2008 10:05]

Re: Problem with data selection [message #63935 is a reply to message #63932 ] Sun, 26 October 2008 10:44 Go to previous messageGo to next message
zonyzeb  is currently offline zonyzeb
Messages: 12
Registered: October 2008
Junior Member
Hello,
I have tried that but I can't fix the issue now it showing some error like...

PropelException]
Error populating Users object [wrapped: Invalid resultset column: 2]
stack trace

* at ()
in SF_ROOT_DIR/lib/model/om/BaseUsers.php line 241 ...
238.
239. return $startcol + 7;
240. } catch (Exception $e) {
241. throw new PropelException("Error populating Users object", $e);
242. }
243. }


Thanks,
Sony Sebastian
Re: Problem with data selection [message #63938 is a reply to message #63927 ] Sun, 26 October 2008 12:48 Go to previous messageGo to next message
whalexis  is currently offline whalexis
Messages: 207
Registered: July 2008
Faithful Member
Hi,

If you define the relation between your tables in your schema.yml file, you should have a method like doSelectJoinXXX in your Peer class.

Alexis
Re: Problem with data selection [message #63944 is a reply to message #63932 ] Sun, 26 October 2008 16:47 Go to previous messageGo to next message
GaryFx
Messages: 377
Registered: May 2008
Location: Masschusetts
Faithful Member
upalom00 wrote on Sun, 26 October 2008 05:03

Try adding in what columns you want to see in you output.

public function execute($request){
$c =new Criteria();
$c->addJoin(UsersPeer::ROLEID,RolesPeer::ROLEID);
$c->add(UsersPeer::USERID,$userId);

//---------
$c->clearSelectColumns();
$c->addSelectColumn(RolesPeer::ROLEID);
//---------

$this->users = UsersPeer::doSelect($c);
}


The Propel doSelect method tries to hydrate the objects that it returns. If you explicitly select a subset of columns the way you're doing with clearSelectColumns and addSelectColumn, then the result set won't have all the fields that doSelect needs to hydrate the objects.

With Propel, you need to understand the differences between the three sorts of select:
  1. doSelect
  2. doSelectRS/doSelectStmt (for Propel 1.2/1.3 respectively)
  3. doSelectJoin

The MyTablePeer::doSelect method will hydrate the MyTable objects. It assumes that it's getting all the fields of the object, that they're the first fields returned, and that they're in the same order as they appear in the schema file. It will ignore any additional fields in the result set. In other words, doSelect doesn't play well with addSelectColumn, clearSelectColumn, or addJoin.

The MyTablePeer::doSelectRS or MyTablePeer::doSelectStmt return the raw result set (for Propel 1.2) or executed PDO statement (for Propel 1.3). These methods don't do any hydration. The 1.3 doSelectStmt is actually very useful if you need specialized results, because of the power of the PDO fetch and fetchAll methods. I suggest reading the PDO docs for this method.

The MyTablePeer::doSelectJoin* methods are the only Propel methods that will hydrate more than one table automatically, as far as I know. And they'll only hydrate two tables (at least in Propel 1.2; I'm not 100% sure about the new features in Propel 1.3).

Bottom line: If you're joining tables, your choices are to either use the doSelectJoin* methods, if there's one that meets your needs, or deal with the unhydrated results.
Re: Problem with data selection [message #63967 is a reply to message #63944 ] Mon, 27 October 2008 05:47 Go to previous messageGo to next message
upalom00  is currently offline upalom00
Messages: 19
Registered: October 2008
Location: California
Junior Member
GaryFx

Thanks so much for this useful explanation!
Re: Problem with data selection [message #90161 is a reply to message #63927 ] Thu, 17 December 2009 16:18 Go to previous message
bdanonymous  is currently offline bdanonymous
Messages: 2
Registered: November 2008
Junior Member
The following code works perfectly for me, you can try



$criteria = new Criteria();
$criteria->addJoin(sfGuardUserProfilePeer::USER_ID,
sfGuardUserPeer::ID );
$criteria->add(sfGuardUserPeer::USERNAME, $name,Criteria::EQUAL);

sfGuardUserPeer::addSelectColumns($criteria);
sfGuardUserProfilePeer::addSelectColumns($criteria);

$stmt= BasePeer::doSelect($criteria);

while( $row = $stmt->fetch(PDO::FETCH_BOTH)){
$row = array_change_key_case($row);

echo $row['username'];

}


Previous Topic:sfRenderException
Next Topic:tasks within a module
Goto Forum:
  

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