samedi 7 novembre 2015

Doctrine2 insert empty fields

I am using Laravel 5.1 and Doctrine 2 to connect to a database. I wrote this entity:

     /**
 * @ORM\Entity
 * @ORM\Table(name="InternalProfile")
 * @ORM\HasLifecycleCallbacks
 */

 class InternalProfile{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @ORM\OneToMany(targetEntity="Person_CreditCard",mappedBy="InternalProfileID")
     * @ORM\OneToMany(targetEntity="InternalProfile_Opinion",mappedBy="InternalProfileID")
     * @ORM\OneToOne(targetEntity="Teacher",inversedBy="InternalProfileID")
     * @ORM\OneToOne(targetEntity="Student",inversedBy="InternalProfileID")
     */
    private $profileid;
    /*
     * @ORM\Column(type="string",unique=true,nullable=false)
     */
    protected $username;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $password;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $name;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $surname;
    /*
     * @ORM\Column(type="date",nullable=false)
     */
    protected $birthdate;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $email;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $sex;
    /*
     * @ORM\Column(type="string",nullable=false)
     */
    protected $address;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $phone;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $img_name;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $postalcode;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $regionarea;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $profiletitle;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $profileoverview;
    /*
     * @ORM\Column(type="string",nullable=true)
     */
    protected $profilebackground;
    /*
     * @ORM\Column(type="string",nullable=false)
     * @ORM\ManyToOne(targetEntity="City")
     */
    protected $city;
    /*
     * @ORM\Column(type="string",nullable=false)
     * @ORM\ManyToOne(targetEntity="City")
     */
    protected $country;
    /*
     * @ORM\Column(name="region-state",type="string",nullable=false)
     * @ORM\ManyToOne(targetEntity="City")
     */
    protected $region_state;

    public function __construct()
    {

    }

    public function getProfileID()
    {
        return $this->profileid;
    }

    public function setUsername($username)
    {
        $this->username=$username;
    }

    public function setPassword($password)
    {
        $this->password=$password;
    }

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

    public function setSurname($surname)
    {
        $this->surname=$surname;
    }

    public function setEmail($email)
    {
        $this->email=$email;
    }

     public function setBirthDate($birthDate)
     {
         $this->birthdate=$birthDate;
     }

    public function setSex($sex)
    {
        $this->sex=$sex;
    }
    public function setAddress($address)
    {
        $this->address=$address;
    }
    public function setPhone($phone)
    {
        $this->phone=$phone;
    }
    public function setImg_name($img_name)
    {
        $this->img_name=$img_name;
    }
    public function setPostalCode($postalcode)
    {
        $this->postalcode=$postalcode;
    }
    public function setRegionArea($regionarea)
    {
        $this->regionarea=$regionarea;
    }
    public function setProfileTitle($profiletitle)
    {
        $this->profiletitle=$profiletitle;
    }
    public function setProfileOverview($profileoverview)
    {
        $this->profileoverview=$profileoverview;
    }
    public function setProfileBackground($profilebackground)
    {
        $this->profilebackground=$profilebackground;
    }
    public function setCity($city)
    {
        $this->city=$city;
    }
    public function setCountry($country)
    {
        $this->country=$country;
    }
    public function setRegion_State($region_state)
    {
        $this->region_state=$region_state;
    }

    public function getUsername()
    {
        return $this->username;
    }
    public function getPassword()
    {
        return $this->password;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getSurname()
    {
        return $this->surname;
    }
    public function getBirthDate()
    {
        return $this->birthdate;
    }
    public function getEmail()
    {
        return $this->email;
    }
    public function getSex()
    {
        return $this->sex;
    }
    public function getAddress()
    {
        return $this->address;
    }
    public function getPhone()
    {
        return $this->phone;
    }
    public function getImg_Name()
    {
        return $this->img_name;
    }
    public function getPostalCode()
    {
        return $this->postalcode;
    }
    public function getRegionArea()
    {
        return $this->regionarea;
    }
    public function getProfileTitle()
    {
        return $this->profiletitle;
    }
    public function getProfileOverview()
    {
        return $this->profileoverview;
    }
    public function getProfileBackground()
    {
        return $this->profilebackground;
    }
    public function getCity()
    {
        return $this->city;
    }
    public function getCountry()
    {
        return $this->country;
    }
    public function getRegion_State()
    {
        return $this->region_state;
    }
 }

Then I have this repository:

class InternalProfileRepository
{
    private $table;
    private $city_table;

    public function __construct()
    {
        $this->table="App\Entity\InternalProfile";
        $this->city_table="App\Entity\City";
    }

    public function addRow($username,$password,$name,$surname,$birthdate,$email,$sex,$address,$phone="NULL",$img_name="NULL",$postalcode="NULL",$regionarea="NULL",$profiletitle="NULL",$profileoverview="NULL",$profilebackground="NULL",$city,$country,$region_state)
    {
        $profile=new InternalProfile();
        $profile->setUsername($username);
        $profile->setName($name);
        $profile->setPassword($password);
        $profile->setSurname($surname);
        $profile->setBirthDate($birthdate);
        $profile->setEmail($email);
        $profile->setSex($sex);
        $profile->setAddress($address);
        $profile->setPhone($phone);
        $profile->setImg_name($img_name);
        $profile->setPostalCode($postalcode);
        $profile->setRegionArea($regionarea);
        $profile->setProfileTitle($profiletitle);
        $profile->setProfileOverview($profileoverview);
        $profile->setProfileBackground($profilebackground);
        $profile->setCity($city);
        $profile->setCountry($country);
        $profile->setRegion_State($region_state);

        $city=EntityManager::getRepository($this->city_table)->findOneBy([
            'name'=>$city,
            'country'=>$country,
            'region'=>$region_state
        ]);
        if($city==NULL)
            return false;
        else
        {
            try {
                EntityManager::persist($profile);
                EntityManager::flush($profile);
                print_r($profile);


                return true;
            }catch (Exception $e){
                print_r($e->getMessage());
                return false;
            }

        }
    }

    public function getRow($profileID)
    {
        try{
            return EntityManager::getRepository($this->table)->findOneBy([
                'profileid' => $profileID
            ]);
        }catch(Exception $e)
        {
            print_r($e->getMessage());
            return NULL;
        }
    }

    public function getAllRows()
    {
        try {
            return EntityManager::getRepository($this->table)->findAll();
        }catch(Exception $e)
        {
            print_r($e->getMessage());
            return NULL;
        }
    }
}

When I try to use the AddRow function of the repository, I have a big lack of data in the truly inserted row. Indeed, it just creates a row with the auto incremented code(profileid) and the other fields set to empty string or null. I checked the value of the object that I create and it contains all the data as I enter it. I have no idea because it acts like that. I also made a test on the persist to check if the object is correctly persisted... And it is. Please help me, I am struggling on this problem...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire