3

TwitterTweets entity:

/**
 * MyBundle\CoreBundle\Entity\TwitterTweets
 *
 * @ORM\Table(name="twitter_tweets")
 * @ORM\Entity
 */
class TwitterTweets
{
    /**
     * @var TwitterUsers
     *
     * @ORM\ManyToOne(targetEntity="TwitterUsers", inversedBy="tweets")
     * @ORM\JoinTable(name="twitter_tweets",
     *   joinColumns={
     *     @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
     *   }
     * )
     */
    private $twitterUser;
}

TwitterUsers entity:

/**
 * MyBundle\CoreBundle\Entity\TwitterUsers
 *
 * @ORM\Table(name="twitter_users")
 * @ORM\Entity
 */
class TwitterUsers
{
    /**
     * @var TwitterTweets
     *
     * @ORM\OneToMany(targetEntity="TwitterTweets", mappedBy="twitterUser")
     */
    private $tweets;
}

twitter_tweets table:

CREATE TABLE `twitter_tweets` (
 `period` int(6) unsigned NOT NULL,
 `tweet_id` varchar(30) NOT NULL,
 `twitter_user_id` bigint(20) unsigned NOT NULL,
 `tweet` varchar(255) NOT NULL,
 `url` text NOT NULL,
 `retweet_count` varchar(10) NOT NULL DEFAULT '0',
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`period`,`tweet_id`),
 KEY `period` (`period`),
 KEY `tweet_id` (`tweet_id`),
 KEY `twitter_user_id` (`twitter_user_id`),
 KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

twitter_users table:

CREATE TABLE `twitter_users` (
 `twitter_id` bigint(20) unsigned NOT NULL,
 `user` varchar(255) NOT NULL,
 `username` varchar(255) NOT NULL,
 `profile_image_url` text NOT NULL,
 PRIMARY KEY (`twitter_id`),
 KEY `user` (`user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I get this error executing a simple SELECT:

$this->getDoctrine()->getRepository('MyBundleCoreBundle:TwitterTweets')->findOneBy(array( 'tweetId' => $data->tweet_id ))

Column not found: 1054 Unknown column 't0.twitterUser_id' in 'field list'

SELECT t0.period AS period1, t0.tweet_id AS tweet_id2, t0.tweet AS tweet3,
t0.url AS url4, t0.retweet_count AS retweet_count5, t0.created_at AS created_at6,
t0.twitterUser_id AS twitterUser_id7
FROM twitter_tweets t0 WHERE t0.tweet_id = ?

How can i solve this issue? I tried to set only the @ORM\JoinColumn (without JoinTable annotation) but i get this error:

"message":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.twittertweets_twittertrends' doesn't exist"

tereško
  • 58,060
  • 25
  • 98
  • 150
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131

2 Answers2

4

Solved using the Many-To-One Unidirectional Association Mapping: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#many-to-one-unidirectional

TwitterTweets entity:

/**
 * MyBundle\CoreBundle\Entity\TwitterTweets
 *
 * @ORM\Table(name="twitter_tweets")
 * @ORM\Entity
 */
class TwitterTweets
{
    /**
     * @var TwitterUsers
     *
     * @ORM\ManyToOne(targetEntity="TwitterUsers")
     * @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
     */
    private $twitterUser;
}

TwitterUsers entity:

/**
 * MyBundle\CoreBundle\Entity\TwitterUsers
 *
 * @ORM\Table(name="twitter_users")
 * @ORM\Entity
 */
class TwitterUsers
{
    // ... no properties needed
}

Thanks to jperovic for his help :)

Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
0

Actually Many-To-Many is defined via@JoinTable. To define Many-To-One you should simply do:

/**
     * @var TwitterUsers
     *
     * @ORM\ManyToOne(targetEntity="TwitterUsers", inversedBy="tweets")
     * @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
     */
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85
  • Thank you for your help, but as i wrote earlier i tried to set only the @ORM\JoinColumn (exactly as in your example) but it doesn't work: **"message":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.twittertweets_twittertrends' doesn't exist"** – Francesco Casula Mar 16 '12 at 19:08
  • 1
    Well, I don't know if I'm blind or something but I totally missed that last part of your question where you wrote about `@JoinColumn`. Sorry about that. Can you update your question with `@Table` annotations you put in the top of your entities? I suspect you either misspelled the table name or didn't put the annotation **at all**... – Jovan Perovic Mar 16 '12 at 19:59
  • I've now added the table annotation at the top of my entities. I assume they are correct. Thanks in advance!! – Francesco Casula Mar 16 '12 at 20:59
  • 1
    Yes, those seem correct. I can't see where `_twittertrends` table name suffix originates. Although both entities belong to the same namespace, is it possible that you have some other entity with same name (from other namespace, obviously)? – Jovan Perovic Mar 16 '12 at 22:30