Question

I am using symfony 1.4 for a project (already started), now I am changing some stuffs and I get an error of relations when I query for some info between two tables, one already exist and the other one is a new one, that should have a foreign key pointing to the first table.

and I get an error message like this

exception 'Doctrine_Table_Exception' with message 'Unknown relation alias ' in /var/www/testorange/symfony/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php:237 

Stack trace: #0 /var/www/testorange/symfony/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php(235): Doctrine_Relation_Parser->getRelation('', false)

...

and many errors more.

So, I think its because my tables have no the proper relations yet on my definition in symfony (they are already related on my DB).

I added this line to my schema.yml then [UPDATED]

 OhrmTrainningSubmit:
  connection: doctrine
  tableName: ohrm_trainning_submit
  columns:
    id_trainning_submit:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    trainning:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    trainning_detail:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    answer:
      type: string(250)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    state:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    user:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    OhrmUser:
      local: user
      foreign: emp_number
      type: many
    OhrmTrainning:
      local: trainning
      foreign: id_trainning
      type: many

this is the definition for OhrmUser

OhrmUser:
  connection: doctrine
  tableName: ohrm_user
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    user_role_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    emp_number:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    user_name:
      type: string(40)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    user_password:
      type: string(40)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    deleted:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    status:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '1'
      notnull: true
      autoincrement: false
    date_entered:
      type: timestamp(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    date_modified:
      type: timestamp(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    modified_user_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    created_by:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
  relations:
    HsHrEmployee:
      local: emp_number
      foreign: emp_number
      type: one
    OhrmUserRole:
      local: user_role_id
      foreign: id
      type: one
    HsHrMailnotifications:
      local: id
      foreign: user_id
      type: many
    OhrmLeaveAdjustment:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveComment:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveEntitlement:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveRequestComment:
      local: id
      foreign: created_by_id
      type: many
    OhrmTimesheetActionLog:
      local: id
      foreign: performed_by
      type: many

On the table where I wanna make the reference, the field that will make the reference is "user" to the table "OhrmUser" to the field emp_number (I think should be correct this way), the problem is that still the same error, does anybody have idea of this? something else I should do?

After I added that I run this

php symfony cc
php symfony doctrine:build-model
php symfony orangehrm:publish-assets
php symfony cc

The query

try{
$q = Doctrine_Query::create()
 ->select('*')
->from('OhrmTrainningSubmit TS')
->innerJoin('TS.OhrmUser U')
->addWhere("TS.trainning = $training")
->andWhere("TS.user = $employee");
$result = $q->execute();
    return $result;
}catch(Exception $e){
    print_r ($e->getMessage());
    return null;
}

I can access to the data of OhrmTrainningSubmit only, but when I try to access to a field of User I get internal error.

I get the error at this code

foreach ($detail as $det){
    echo $det['answer']; // This is printed with no problem
    //echo $det['user_name']; <-- this one comes from the table OhrmUser, I get server error with this one
}

where $detail is the variable with the return value of the query.

When I make

->select('TS.*, U.*')

I get other error, that says

Unknown property emp_number

any idea?

Thanks in advance.

Was it helpful?

Solution

Two things:

  1. In your schema make sure you have the correct indentation near relations:

      autoincrement: false
    relations:
      OhrmUser:
        local: user
        foreign: emp_number
        type: many
    
  2. The query should look like this:

    $q = Doctrine_Query::create()
        ->from('TrainningSubmit T')
        ->innerJoin('T.OhrmUser U')
        ->where('T.id_trainning = ?', $id);
    

When you specify the join in doctrine you must tell the ORM where does the relation come from - thus T.OhrmUser U instead of OhrmUser U. This tells doctrine that you are using the OhrmUser relation from the T model.

What is more you used ->where() twice which is wrong because the second where overwrites the first one. You should use ->andWhere() or ->orWhere() if you want to add more than one where condition. Nevertheless when dealing with named relations in doctrine you don't have to tell doctrine explicitly on what columns it should make the join - it already knows that thanks to the schema file.

What is more, if you need to extend the condition of the join you can do that easily using WITH, e.g.

 ->join('T.OhrmUser U WITH U.name LIKE ?', 'Michal')

will translate to:

 JOIN OhrmUser U ON U.emp_number = T.user AND U.name LIKE 'Michal'

EDIT

To retrieve data from the object use:

//if you return a plain array as a result of the query:
echo $det['answer'];
echo $det['OhrmUser']['user_name'];

//if you return an array of objects:
echo $det->getAnswer();
echo $det->getOhrmUser()->getUserName();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top