Why your setup function doesn't specify a relation?

On 11 bře, 22:39, Jim Krehl <jim...@harmonixmusic.com> wrote:
> I'm having some problems with using relations and
> doctrine:generate-admin.  I have two tables, call them Users and
> Umbrellas.  There is a one-to-many relation between Users and Umbrellas,
> specifically ...
>
> class Umbrella extends sfDoctrineRecord {
>      public function setUp ()
>      {
>          $this->hasOne ('User as owner', array ('local' => 'owner_id',
> 'foreign' => 'id'));
>      }
>
> }
>
> The User::setUp function does not specify a relation with Umbrella.
>
> I want to make an admin interface that allows admins to create Umbrella
> objects.  The first issue is that the widget created to input the
> owner_id is an sfWidgetFormDoctrineChoice which makes a select-type
> input with every possible user id.  This is undesirable for 2 reasons,
> first there are far too many users (> 100,000) to populate a select-type
> input.  Secondly, it is much more intuitive to specify a user by
> username instead of by numeric id.
>
> I took care of this first issue by overriding the auto-generated
> widget/validator, i.e. ...
>
> class UmbrellaForm extends BaseUmbrellaForm {
>      public function configure ()
>      {
>          parent::configure ();
>
>          $this->setWidget    ('owner_id', new sfWidgetFormInput ());
>          $this->setValidator ('owner_id', new UserValidator ());
>      }
>
>      public function updateDefaultsFromObject ()
>      {
>          parent::updateDefaultsFromObject ();
>
>          $this->setDefault ('owner_id', $this->getObject
> ()->owner->username;
>      }
>
> }
>
> class UserValidator extends sfValidatorString {
>      public function doClean ($value)
>      {
>          $value = parent::doClean ($value);
>
>          $user = UserTable::getByUsername ($value);
>
>          /* check if a valid user, check if user is allowed to own an
> umbrella, etc. */
>
>          return $user->id;
>      }
>
> }
>
> The above works pretty handily for associating an existing user with an
> umbrella.  I'm including it in this post in case I'm doing something
> improperly or if it's complicating my unresolved issue, which is ... I
> am not able to properly create _new_ Umbrella objects with the admin
> interface.
>
> One of the first things that the admin interface does after receiving a
> new umbrella form submission is to create a new, empty Umbrella object
> _and_ a new, empty User object.  When the admin inputs a username for
> the owner_id, instead of just inserting a new umbrella row with the
> owner_id set, it also tries to insert an entirely new user row into the
> database with an already extant username.
>
> So, my question is ... How can I convince the admin interface not to
> create a new instance of a foreign relation?
>
> Thanks,
> Jim Krehl

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to