[symfony-users] Re: Many2Many add relation?

2010-04-30 Thread comb
ok.. shame on me.. it works with:
$author_tutorial = new AuthorTutorial();
$author_tutorial->author_id = $author->getId();
$author_tutorial->tutorial_id = $tutorial->getId();
$author_tutorial->save();

But how do I check for duplicates - if the $author is already linked
with the tutorial? Do I need to do a SELECT manually or is their some
function like: $tutorial->hasAuthor($author)?

On 30 Apr., 23:40, comb  wrote:
> Hi!
>
> I have a tutorial and author model in a many-to-many relation
> (AuthorTutorial).
> Now I want to add a author to a concrete tutorial which already has
> authors, without duplicates.
> How do I do that?
>
> I tried s/th like this:
>         // temporarily save old authors:
>         $authors = $tutorial->getAuthors();
>         // remove all authors:
>         $tutorial->AuthorTutorial->delete();
>         // add author:
>         $authors[] = $current_author;
>         // add all authors again:
>         $trick->setAuthors($authors);
>
> But this ends up in a "SQLSTATE[23000]: Integrity constraint
> violation: 1048 Column 'author_id' cannot be null"-Error. :-(
> If I use $authors[] = $current_author->getId(); instead i get a "Fatal
> error: Call to a member function save() on a non-object"
>
> Any help will be appreciated
> comb
>
> --
> 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 
> athttp://groups.google.com/group/symfony-users?hl=en

-- 
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


Re: [symfony-users] Re: Many2Many add relation?

2010-04-30 Thread Stéphane
Hi,

I never played with ArrayAccess interfaces.

$authors = clone $tutorial->get('authors'); //might not work, dunno if
Doctrine_Collection have its __clone method; anyway its not the purpose
$tutorial->get('authors')->delete(); //delete associations from this tuto to
all its authors
$tutorial->get('authors')->add($new_author); //must have $new_author->save()
== 1 for object to have an ID; (you can test
->state()==Doctrine_Record::CLEAN to know if you have to save it); its the
responsability of the Doctrine_Collection object given by ->get('authors')
-when configured correctly and well used- to understand that it is a m:m
association class and to act this way (thus matching what have to be
matched; you'll not have two equivalent record-association ie two
AuthorTutorial with the same values until you really want and code it -like
using versionable on this association class, dunno if working btw-).

About the error, you might try to save an object which is in fact an array
(hydrating a record as an array somewhere and using it as an object ?).

Cheers,


Before Printing, Think about Your Environmental Responsibility!
Avant d'Imprimer, Pensez à Votre Responsabilitée Environnementale!


On Fri, Apr 30, 2010 at 11:57 PM, comb  wrote:

> ok.. shame on me.. it works with:
> $author_tutorial = new AuthorTutorial();
> $author_tutorial->author_id = $author->getId();
> $author_tutorial->tutorial_id = $tutorial->getId();
> $author_tutorial->save();
>
> But how do I check for duplicates - if the $author is already linked
> with the tutorial? Do I need to do a SELECT manually or is their some
> function like: $tutorial->hasAuthor($author)?
>
> On 30 Apr., 23:40, comb  wrote:
> > Hi!
> >
> > I have a tutorial and author model in a many-to-many relation
> > (AuthorTutorial).
> > Now I want to add a author to a concrete tutorial which already has
> > authors, without duplicates.
> > How do I do that?
> >
> > I tried s/th like this:
> > // temporarily save old authors:
> > $authors = $tutorial->getAuthors();
> > // remove all authors:
> > $tutorial->AuthorTutorial->delete();
> > // add author:
> > $authors[] = $current_author;
> > // add all authors again:
> > $trick->setAuthors($authors);
> >
> > But this ends up in a "SQLSTATE[23000]: Integrity constraint
> > violation: 1048 Column 'author_id' cannot be null"-Error. :-(
> > If I use $authors[] = $current_author->getId(); instead i get a "Fatal
> > error: Call to a member function save() on a non-object"
> >
> > Any help will be appreciated
> > comb
> >
> > --
> > 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 athttp://
> groups.google.com/group/symfony-users?hl=en
>
> --
> 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
>

-- 
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