logo
down
shadow

Symfony2 - Collection of Forms - Update -ManytoMany-Relation


Symfony2 - Collection of Forms - Update -ManytoMany-Relation

By : Exotic Serendipity
Date : November 17 2020, 11:52 AM
This might help you I'm very new to Symfony2 and now I have a problem with a collection of forms. I have two Entities. The Entity User and the Entity Role. , You should have in your user entity :
code :
    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     * @ORM\JoinTable(name="role_assigned_to_user",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     *      )
     */
    private $roles;
    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
     */
    private $users;
    public function __construct() {
       $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }
->add('roles', 'entity', array(
            'class' => 'YourBundle:Role',
            'property' => 'name',
        ))


Share : facebook icon twitter icon
Symfony2-Doctrine: ManyToMany relation is not saved to database

Symfony2-Doctrine: ManyToMany relation is not saved to database


By : user3412115
Date : March 29 2020, 07:55 AM
help you fix your problem Your Category entity is the inverse side of the relationship.
Try changing addItems to look like this:
code :
public function addItem(\Ako\StoreBundle\Entity\Item $item)
    {
        $item->addCategory($this);
        $this->items[] = $item;
    }
Symfony2: ManyToMany relation and Collection form type

Symfony2: ManyToMany relation and Collection form type


By : Ala'a AL-Odiebat
Date : March 29 2020, 07:55 AM
Hope that helps Well, I don't deal much with doctrine, but seems that you are inserting a tag with the same ID/Name in the same collection. I think you could check the tag existence before insert
code :
public function existTags(\Doctrine\Common\Collections\ArrayCollection $tags)
{
    foreach($this->tags as $tag)
    {
        if ( $tag->getID() === $tags->getId() )
            return true;
    }
    return false;
}
public function addTag(\My\BlogBundle\Entity\Tag $tags)
{
    if ( !$this->existTag($tags) );
        $this->tags[] = $tags;
}
namespace models;


/**
 * @Entity
 * @Table(name="tag")
 */
class Tag
{

    /**
     * @Id @Column(type="integer", nullable=false, name="id")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @Column(type="string", nullable=false)
     */
    protected $name;

    public function getId(){ return $this->id; }
    public function getName(){ return $this->name; }

    public function setId($id){ $this->id = $id; }
    public function setName($name){ $this->name = $name; }


}
namespace models;

use \Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="entry")
 */
class Entry
{

    /**
     * @Id @Column(type="integer", nullable=false, name="id")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="string", nullable=false, name="body")
     */
    protected $body;



    /**
     * @ManyToMany(targetEntity="Tag")
     * @JoinTable(name="entry_tagged",
     *      joinColumns={@JoinColumn(name="entry_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    private $tags;





    public function __construct(){
        $this->tags = new ArrayCollection();
    }

    public function existTag(models\Tag $tag)
    {
        foreach($this->tags as $temp)
        {
            if ( $tag->getID() === $temp->getId() )
                return true;
        }
        return false;
    }

    public function addTag(models\Tag $tag)
    {
        if ( !$this->existTag($tag) );
            $this->tags->add($tag);
    }


    public function getId(){ return $this->id; }
    public function getBody(){ return $this->body; }
    public function getTags(){ return $this->tags; }

    public function setId($id){ $this->id = $id; }
    public function setBody($body){ $this->body = $body; }
    public function setTags($tags){ $this->tags = $tags; }

}
entry_id | tag_id
Symfony2 doctrine ManyToMany relation

Symfony2 doctrine ManyToMany relation


By : tiok_cek
Date : March 29 2020, 07:55 AM
it should still fix some issue First I would recommend to use more natural language names for you relationships, like disciplines and endUsers since it is a manyToMany bond. Also, you need to create the get and set method for each. After you have prepared all your properties for an entity you should run the command to Generate your getters and setters
code :
//this will generate all entities
php app/console doctrine:generate:entities BundleNamespace
$endUser->getDisciplines(); //return all disciplines of this user
$endUser->addDiscipline($someDiscipline); //add another discipline
$endUser->removeDiscipline($iAmABadDiscipline); //remove this discipline from this user
array $disciplines = [ ... ];
$endUser->setDisciplines($disciplines); // set multiple disciplines
Symfony2, get elements from manytomany relation in controller

Symfony2, get elements from manytomany relation in controller


By : s1dney
Date : March 29 2020, 07:55 AM
This might help you The preferences will be set to a Doctrine\Common\Collections\ArrayCollection containing instances of Preferences. So you can just loop over them as if they were an array.
code :
$preferences = array();
foreach ($result->getPreferences() as $preference) {
    $preferences[] = $preference->getName();
}
$preferenceString = implode(' - ',$preferences);
Symfony2, relation manyToMany in a Form

Symfony2, relation manyToMany in a Form


By : Christian Kim
Date : March 29 2020, 07:55 AM
With these it helps I found it, for persons who have the same problem, in the relation ManyToMany you need to have multiple=>true in your FormType, so my MediaType should be:
Related Posts Related Posts :
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • Error with the ereg_replace
  • Getting the fields attached to a bundle of an entity in Drupal
  • Php how to check multiple times (within `foreach`) if values from array exists in mysql?
  • MySQL and PHP parsing strange string
  • Pull specific data from multidimensional associative array
  • How to get the original URI extension using PHP Tonic?
  • Upload videos and images from Android to PHP server?
  • Unset a multidimensional array with another multidimensional array with values than key
  • Calling a user e-mail to add to a mailto link PHP
  • Why is override_function coming back as an undefined function
  • PHP mySQLi_fetch_all: iterate through each row
  • Using pow in php
  • What's wrong with my PHP Script? T_String issue
  • how to call a function of another file without including that file
  • is_rgb function in php
  • transform the numbers to letters using php
  • How to catch an exception from another class method PHP
  • PHP exec is blocked by ISP
  • PHP4 to PHP5 migration character encoding. latin1 vs utf8
  • Begin Transaction not defined
  • How much harm can a user do with xss on a page unique to them?
  • How to import data from a php file to javascript variable
  • PHP and Javascript - log complete workflow
  • When I echo !!! 0 it gives me a strange result
  • jquery ajax post dies after submit
  • How to upload file with curl on sftp server
  • Advanced search using a text input to search through only one category
  • Why do I get "Non-static method App\Models\Category::products() should not be called statically" in laravel
  • Stop notices from undefined indexes in arrays
  • PHP Excel Error when entering SUM function to a cell
  • laravel 4 eloquent eager load relation count
  • click submit in html and run php code
  • Extremely frustrating behavior with Internet Explorer, Bootstrap and Respond.js
  • Vkontakte vtag issues
  • Divide the number to a correct decimal
  • mysqli_insert_query not working
  • How to make a sitemap using zend framework 1
  • Laravel: array to Model with relationship tree
  • $_SERVER['DOCUMENT_ROOT'] vs $path = $_SERVER['DOCUMENT_ROOT']
  • What is the best way to store multiple checkbox values in the database and work with it later on
  • Why does foreach copy the array when we did not modify it in the loop?
  • Receive Jquery/Ajax Post Request in PHP
  • How to better use the if(!empty()){ //codes} statement
  • PHP error mysql_close() boolean given
  • Move an image file from one server to another using PHP
  • PHP: Encoding non ISO-8859-1 charactrers into utf8
  • Handling MySQL errors in PHP
  • Object orientated mysql num_rows query in one line
  • Remove not closed html elements from end of text
  • Generate random ENUM value [PHP, MySQL]
  • Deleting data from table using jQuery and SQL
  • How to make sure the constructor is executed when I'm trying to reach a static property?
  • Trying to understand arrow next to PHP function
  • Form using external php file to show "Sucess"
  • WooCommerce - new shipping method - how to query individual product weights
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org