Google Groups Home
Help | Sign in
Unique rows with auto increment
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Matt Hulse  
View profile
 More options May 15, 6:22 pm
From: Matt Hulse <matt.hu...@gmail.com>
Date: Thu, 15 May 2008 15:22:18 -0700 (PDT)
Local: Thurs, May 15 2008 6:22 pm
Subject: Unique rows with auto increment
I have a permissions table which has two fields, page and action.  I
don't want duplicate permission entries but that's what I'm getting
since Doctrine wants an auto increment id field.  How do you handle
this kind of thing?

I would really like to setup something like a singleton where I create
a new permission object and when I save, if an entry with the same
page and action already exists, it returns that entry, otherwise it
creates a new entry and returns it.

$permission = new Permission();
$permission->page = 'accounts';
$permission->action='create';
$permission->save(); //permission id = 3

$permission = new Permission();
$permission->page = 'accounts';
$permission->action='delete';
$permission->save();//permission id = 4

$permission = new Permission();
$permission->page = 'accounts';
$permission->action='create';
$permission->save();//permission id = 3

I would like to implement this in a way that will allow me to use
something like $permission->merge($_POST).  Does this imply overriding
save?

I appreciate any suggestions.  Thanks in advance.

Matt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Wage  
View profile
 More options May 15, 7:49 pm
From: "Jonathan Wage" <jonw...@gmail.com>
Date: Thu, 15 May 2008 18:49:12 -0500
Local: Thurs, May 15 2008 7:49 pm
Subject: Re: [doctrine-user] Unique rows with auto increment

That would work or you could do it with a preSave() record listener
possibly. Read up on listeners and you will probably find what you want.

- Jon

--
Jonathan Wage
http://www.jwage.com
http://www.centresource.com

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Hulse  
View profile
 More options May 15, 11:32 pm
From: Matt Hulse <matt.hu...@gmail.com>
Date: Thu, 15 May 2008 20:32:01 -0700 (PDT)
Local: Thurs, May 15 2008 11:32 pm
Subject: Re: Unique rows with auto increment
Jon, thanks for the response.

I explored listeners and hooks and here is what I came up with:

    public function preInsert($event)
    {
        //if a record with this controller and action exists,
        // don't create a new one
        $table = Doctrine::getTable("Permission");
        $permission = $table->findByDql("controller = ? and action = ?",
                                                        array($this->controller, $this->action));
        if($permission[0]->exists()) {
                //echo "Already exists.";
                $this->id = $permission[0]->id;
                $event->skipOperation();
        }
    }

This code seems to do what I need and duplicate permissions are not
created.  There is a problem though when I try to create permissions
with relation to the user table.

  $user = new User();
  $user->Permissions[0]->controller = "account";
  $user->Permissions[0]->action = "create";
  $user->save();

This code generates a new entry for my associative table between users
and permissions as expected, but now permission_id is always null,
even for the initial insert.  Do I need to change something in the
user model?

Any ideas?

Thanks,

Matt

On May 15, 5:49 pm, "Jonathan Wage" <jonw...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Wage  
View profile
(1 user)  More options May 16, 2:59 pm
From: "Jonathan Wage" <jonw...@gmail.com>
Date: Fri, 16 May 2008 13:59:36 -0500
Local: Fri, May 16 2008 2:59 pm
Subject: Re: [doctrine-user] Re: Unique rows with auto increment

In your listener use $this->assignIdentifier($permission[0]->id); instead of
$this->id = $permission[0]->id;

- Jon

--
Jonathan Wage
http://www.jwage.com
http://www.centresource.com

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google