Welcome Guest, Not a member yet? Register   Sign In
Doctrine Model one-to-many relationship
#1

[eluser]TheJonas[/eluser]
Hi everybody,

I'm having a pretty tough time getting a 1:n association with Doctrine to work.

Basically, there is a Relationship and a RelationshipType with each Relationship having one RelationshipType and each RelationshipType having pot. many Relationships.

My Relationship.php (model) looks like this:
Code:
/** @Entity
*  @Table(name="relationship")
*
*/
class Relationship extends CI_Model
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    /**
     * @ManyToOne(targetEntity="RelationshipType", inversedBy="relationships")
     * @JoinColumn(name="fk_relationship_type_id", referencedColumnName="id")
     */
    private $relationshipType;


}

And my RelationshipType.php looks like this:
Code:
/** @Entity
*  @Table(name="relationship_type")
*
*/
class RelationshipType extends CI_Model
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    private $id;

    /**
     * @oneToMany(targetEntity="Relationship", mappedBy="relationshipType")
     */
    private $relationships;
    
    /**
     * @Column(name="relationship_name", length=255)
     */
    private $relationshipName;


    public function __construct()
    {
        $this->relationships = new \Doctrine\Common\Collections\ArrayCollection();
    }

My tables look like this:
Code:
--
-- Table structure for table `relationship`
--

CREATE TABLE IF NOT EXISTS `relationship` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fk_from_person_id` int(11) NOT NULL,
  `fk_to_person_id` int(11) NOT NULL,
  `fk_relationship_type_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `relationship`
--

INSERT INTO `relationship` (`id`, `fk_from_person_id`, `fk_to_person_id`, `fk_relationship_type_id`) VALUES
(1, 1, 2, 1),
(2, 1, 3, 1);

-- --------------------------------------------------------

--
-- Table structure for table `relationship_type`
--

CREATE TABLE IF NOT EXISTS `relationship_type` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `relationship_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `relationship_type`
--

INSERT INTO `relationship_type` (`id`, `relationship_name`) VALUES
(1, 'friendship');

The problem definitely is the 1:n association as if I comment out the field then everythign works fine, if I run the code as posted here I get the following error:

Quote:A PHP Error was encountered

Severity: Warning

Message: file_put_contents(application//models/proxies\RelationshipTypeProxy.php) [function.file-put-contents]: failed to open stream: No such file or directory

Filename: Proxy/ProxyFactory.php

Line Number: 154
A PHP Error was encountered

Severity: Warning

Message: require(application//models/proxies\RelationshipTypeProxy.php) [function.require]: failed to open stream: No such file or directory

Filename: Proxy/ProxyFactory.php

Line Number: 85

Fatal error: require() [function.require]: Failed opening required 'application//models/proxies\RelationshipTypeProxy.php' (include_path='.;C:\Server\xampp\php\PEAR') in C:\Server\xampp\htdocs\CI\application\libraries\Doctrine\ORM\Proxy\ProxyFactory.php on line 85

Can anybody point me into the direction of my mistake? I've been reading the Doctrine docs but somehow I can't seem to find the problem :-(

Thank you very much in advance!
#2

[eluser]TheJonas[/eluser]
Hmm... I think I partly found the problem - if I change the fetching type of the relationship explicitly to eager it works. Apparently there is a problem with the lazy loading because there are no proxy objects.

However, I do have $config->setAutoGenerateProxyClasses( TRUE ); in my Doctrine.php and from my understanding that should be enough to generate the proxy classes and load these. Any ideas on where the problem could be? And is there a way how can I see if the proxy classes are being generated properly? I tried searching my file system for files including "proxy" in their name and didn't come up with anything. I did run the doctrine console with the orm:generate-proxies command and it executed without writing anything to the console window...

Thanks!
#3

[eluser]Unknown[/eluser]
I just had the same problem.

Here is my solution:

Check if your proxies folder exists, because doctrine will not create folders for you!
You will find the folder definition in Doctrine.php

My conf looks like this
Code:
$config->setProxyDir(APPPATH . 'models/Proxies');

So i created folder /models/Proxies and it all started to work.

Hope this helps




Theme © iAndrew 2016 - Forum software by © MyBB