Ask your Symfony questions! Pay money and get answers fast! (more info)

How can I make the model class a variable, with Propel? Symfony

  • SOLVED

Suppose I have code like this:


$c = new Criteria();
$c->add(WssOracleUserCoursePeer::USER_ID, $userid);
$c->add(WssOracleUserCoursePeer::COURSE_ID, $courseId);
$userCourse = WssOracleUserCoursePeer::doSelectOne($c);
if ($userCourse->getStatus() == "C") {
return false;
}


Now I want to change WssOracleUserCoursePeer to a variable that changes depending on circumstance. Simply doing this gives parse errors:


$c = new Criteria();
$c->add($model::USER_ID, $userid);
$c->add($model::COURSE_ID, $courseId);
$userCourse = $model::doSelectOne($c);
if ($userCourse->getStatus() == "C") {
return false;
}


and this also fails:


$c = new Criteria();
$c->add({$model}::USER_ID, $userid);
$c->add({$model}::COURSE_ID, $courseId);
$userCourse = {$model}::doSelectOne($c);
if ($userCourse->getStatus() == "C") {
return false;
}



So how do I do this?

Answers (4)

2010-06-25

Pascal answers:

Hi, you can try :


$c = new Criteria();

$c->add(constant($model . '::USER_ID'), $userid);
$c->add(constant($model . '::COURSE_ID'), $courseId);
$userCourse = call_user_func(array($model, 'doSelectOne'), $c);
if ($userCourse->getStatus() == "C") {
return false;
}

2010-06-25

Arturo Linares answers:

That is fixed on PHP 5.3.

Try using the function constant($model.'::USER_ID'); in prior versions.

2010-06-25

Francesco Tassi answers:

it's not really propel related but php related:

you can get constant value using

constant($model.'::COURSE_ID');

and call the doSelectXXX methods using (not sure if both work)

call_user_func($model.'::doSelectOne', $c);
call_user_func(array($method, 'doSelectOne'), $c);

2010-06-25

GlobalOrangeLab answers:

Try this:

$oCriteria = new Criteria();

$oCriteria->add(constant($model . '::USER_ID'), $userid);

$oCriteria->add(constant($model . '::COURSE_ID'), $courseId);

$userCourse = call_user_func(array($model, 'doSelectOne'), $oCriteria);

if ($userCourse->getStatus() == "C") {

return false;

}


Hope this helps you!