Yii MANY_MANY on子句

时间:2013-05-15 22:32:31

标签: database join yii

我的数据库中有以下表格。

subscriber(
    subscriber_id int, 
    status char(1),
    ...
)

sendlist(
    sendlist_id int,
    ...
)

subscriber_list(
   subscriber_id int,
   sendlist_id int,
   status char(1)
)

在我的SendList模型中,我的关系定义如下:

'Subscribers'=>array(self::MANY_MANY, 'Subscriber', 'subscriber_list(sendlist_id, subscriber_id)',"on"=>"Subscribers_Subscribers.status='a' and Subscribers.status='a'")

我已尝试同时设置on和condition子句以及其中一个,但是目前表中唯一的订阅者(链接到此发送列表)总是被返回,无论它是否在订阅者表中的状态,或者subscriber_list表设置为's'。

当我检查数据库时 - 该连接的查询应该提供以下内容:

SELECT `Subscribers`.`subscriber_id` AS `t1_c0`, `Subscribers`.`full_phone` AS `t1_c1`, `Subscribers`.`status` AS `t1_c2`, `Subscribers`.`contact` AS `t1_c3`, `Subscribers`.`client_id` AS `t1_c4`, `Subscribers`.`date_joined` AS `t1_c5` FROM `subscriber` `Subscribers`
INNER JOIN `subscriber_list` `Subscribers_Subscribers` ON (`Subscribers_Subscribers`.`sendlist_id`=7075) AND (`Subscribers`.`subscriber_id`=`Subscribers_Subscribers`.`subscriber_id`) AND (Subscribers.status='a' and Subscribers_Subscribers.status='a') 
WHERE (Subscribers.status='a' and Subscribers_Subscribers.status='a');

返回一个空集(应该如此)。但是当我print_r $ list-> Subscribers数组时,我得到以下内容:

Array(
[0] => Subscriber Object
    (
    ...
            [_attributes:CActiveRecord:private] => Array
            (
                [subscriber_id] => 2043221
                [full_phone] => 447944426885
                [status] => s
                [contact] => 0
                [client_id] => 14002
                [date_joined] => 
            )

所以它认识到订阅者是状态'',但仍然加载它!

导致问题的代码在这里:

  $client = new Client();
  $client->save(false);

  //ensure that we are working with a clean subscriber set.
  $subscribers = Subscriber::model()->findAll();
  foreach($subscribers as $subscriber){
    $subscriber->delete();
  }

  //create a new subscriber with a set of filter options.
  $subscriber = new Subscriber();
  $subscriber->client_id = $client->client_id;
  $subscriber->full_phone = "44712345678";
  $subscriber->country = "England";
  $subscriber->gender = "Male";
  $subscriber->save(false);


  //create a new list with a set of matching options
  $list = new SendList();
  $list->client_id = $client->client_id;
  $list->gender = "=Male";
  $list->save(false);

  //confirm that the subscriber can be added to the list.
  self::assertTrue($list->canImport($subscriber));
  $list->import();

  //confirm that the subscriber has been added to the list.
  self::assertEquals(1, $list->active_subscriber_count);
  self::assertEquals(1, sizeof($list->Subscribers));

  //remove all subscribers (set status to 's')
  $list->remove();

  //check that the list no longer records the subscribers presence.
  self::assertEquals(0, $list->active_subscriber_count);
  self::assertEquals(1, $list->stopped_subscriber_count);

  //check that the subscriber has no lists associated with it.
  $subscriber->refresh();
  self::assertEquals('s', $subscriber->status, sizeof($subscriber->SendLists));

  //attempts to refresh the list. Tested without these with the same result.
  $list_id = $list->sendlist_id;
  unset($list);
  unset($subscriber);
  $list2 = SendList::model()->findByPk($list_id);
  $list2->refresh();

  //check that the the list has no subscribers - this fails.
  self::assertEquals(0, sizeof($list2->Subscribers));

我错过了一些愚蠢的事吗?

0 个答案:

没有答案
相关问题