I am trying to create dynimic model class with a model function inside the user model, however for some reason it unable to identify the class location. it gives me an error
Fatal error: Class 'VarEducation' not found
below is the function
public function partner()
{
$view_service=new ViewService();
$partner_vars_check=$view_service->getUserVarPartnerModelCheckMappingArray();
foreach($partner_vars_check as $partner_key=>$partner_var){
$table= str_replace("p_","var_",$partner_key);
$sql = "SELECT * FROM ".$table."
WHERE id='" . $this->{$partner_key} . "'
ORDER BY id DESC";
$partner_obj = new $partner_var();
$this->{$partner_key} = new \Phalcon\Mvc\Model\Resultset\Simple(null, $partner_obj,
$partner_obj->getReadConnection()->query($sql));
}
}
Any idea on what causing this ?
data supplied by the view service is as below
public function getUserVarPartnerModelCheckMappingArray() {
return array(
'p_education' => 'VarEducation',
'p_body' => 'VarBody',
'p_ethnicity' => 'VarEthnicity',
'p_religion' => 'VarReligion',
'p_family' => 'VarFamily',
);
}
Whatever you do, if you have model hidden in any namespace, you have to deliver full namespaces with your mapping array. Lets say, all your models are in namespace of Application\Models, you have to use it this way:
public function getUserVarPartnerModelCheckMappingArray() {
return array(
'p_education' => '\Application\Models\VarEducation',
'p_body' => '\Application\Models\VarBody',
'p_ethnicity' => '\Application\Models\VarEthnicity',
'p_religion' => '\Application\Models\VarReligion',
'p_family' => '\Application\Models\VarFamily',
);
}
Not saying that I would rather switch/case over such a small amount of options and use models directly, instead of building SQL by hand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With