문제

In my database, I have a user table. This user table has several fields, amongst them parent_user_id and user_class_id, both foreign keys, neither of them are part of the primary key. In my BaseUser.php, it lists the foreign keys as follows:

protected $aUserRelatedByUserParentId;
protected $aUserClass;

Similarly, I have getUserClass() and setUserClass() but I do not have getUserParent() and setUserParent(). Rather, I have getUserParentId() and setUserParentId(). Is there a reason for this and if so, can I override it in the schema.xml? Here it is:

<?xml version="1.0" encoding="utf-8"?>
<!--Autogenerated by PropelSchemaReverseTask class.-->
<database name="test" defaultIdMethod="native">
  <table name="user" phpName="User" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" required="true"/>
    <column name="parent_user_id" phpName="ParentUserId" type="INTEGER" required="true"/>
    <column name="name" phpName="Name" type="VARCHAR" size="45" required="true"/>
    <column name="description" phpName="Description" type="VARCHAR" size="45" required="true"/>
    <column name="user_class_id" phpName="UserClassId" type="INTEGER" required="true"/>
    <foreign-key foreignTable="user_class" name="fk_user_class">
      <reference local="user_class_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="user" name="fk_user_parent">
      <reference local="parent_user_id" foreign="id"/>
    </foreign-key>
    <index name="fk_user_parent_idx">
      <index-column name="parent_user_id"/>
    </index>
    <index name="fk_user_class_idx">
      <index-column name="user_class_id"/>
    </index>
    <vendor type="mysql">
      <parameter name="Engine" value="InnoDB"/>
    </vendor>
  </table>
  <table name="user_class" phpName="UserClass" idMethod="native">
    <column name="id" phpName="Id" type="INTEGER" primaryKey="true" required="true"/>
    <column name="name" phpName="Name" type="VARCHAR" size="45" required="true"/>
    <column name="permissions" phpName="Permissions" type="VARCHAR" size="45" required="true"/>
    <vendor type="mysql">
      <parameter name="Engine" value="InnoDB"/>
    </vendor>
  </table>
</database>
도움이 되었습니까?

해결책

I've just generated your schema in my own Propel environment, and found that you should currently have this:

/* @var $user User */
$user = new User();
$user->getUserRelatedByParentUserId();
$user->getUserClass();

I notice that you have phpName attributes on various columns - I'd recommend removing them, since you're requesting the defaults that would be generated anyway.

However, you're on the right track - try this:

<foreign-key foreignTable="user" name="fk_user_parent" phpName="ParentUser">
  <reference local="parent_user_id" foreign="id"/>
</foreign-key>

I'm assuming you're happy with the default relation name given to UserClass, so that doesn't change. However, for the self-join, I've added a custom relation name, which should give you $user->getParentUser().

Incidentally, I expect you're using the name attributes on the FK definitions to give custom names to your constraints. That's fine if you want control over this, but I tend to let Propel name them as it wishes. If you are happy with that, they can be removed from the schema.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top