doumei4964 2016-07-11 10:56
浏览 14
已采纳

用Propel更新对象后,我可以保持水合作用吗?

If I fetch an object like so:

$q = OrderReturnQuery::create()
    ->joinWith('Type')
    ->joinWith('Status')
    ->useStatusQuery()
        ->joinWith('Email')
        ->endUse()
    ->joinWith('Priority');
$object = $q->findPk(1);
var_dump($object->toArray(TableMap::TYPE_PHPNAME, true, [], true));

This is the output I get:

array (size=14)
  'Id' => int 1
  'TypeId' => int 3
  'StatusId' => int 2
  'PriorityId' => int 1
  'OrderId' => int 234567
  'CustomerId' => int 5
  'Initiated' => string '2016-03-02T01:11:12+00:00' (length=25)
  'Initiator' => int 2
  'FreePostageLabel' => boolean true
  'LostInPost' => boolean false
  'SuppressEmail' => boolean true
  'Type' => 
    array (size=4)
      'Id' => int 3
      'Title' => string 'title 3' (length=7)
      'Priority' => int 3
      'OrderReturns' => 
        array (size=1)
          0 => string '*RECURSION*' (length=11)
  'Status' => 
    array (size=6)
      'Id' => int 2
      'EmailId' => int 2
      'Title' => string 'title 2' (length=7)
      'Priority' => int 2
      'Email' => 
        array (size=5)
          'Id' => int 2
          'Subject' => string 'subject 2' (length=9)
          'Plaintext' => string 'plain text 2' (length=12)
          'Html' => string 'html 2' (length=6)
          'Statuses' => 
            array (size=1)
              0 => string '*RECURSION*' (length=11)
      'OrderReturns' => 
        array (size=1)
          0 => string '*RECURSION*' (length=11)
  'Priority' => 
    array (size=4)
      'Id' => int 1
      'Title' => string 'title 1' (length=7)
      'Priority' => int 1
      'OrderReturns' => 
        array (size=1)
          0 => string '*RECURSION*' (length=11)

Now if I modify the original code to change a value before dumping:

$object = $q->findPk(1);¬
$object->setStatusId(5);

The resulting output doesn't include the Status element, only the 'StatusId'. I can of course get this back with $object->getStatus() before using toArray() but is there a way to do this generically?

I was wondering if there's a way to check if a value is a foreign key so that if it is I can automatically get getWhatevers() after that value is set, instead of hard-coding them. Or maybe there's a better way?

My other option is to override toArray but the scope for errors and amount of maintenance as a database changes here would have to be considered.

  • 写回答

1条回答 默认 最新

  • doukun0888 2016-07-14 12:35
    关注

    When you're calling the toArray method the fourth parameter can be set to true in order to make the model retrieve the related objects

    $object->toArray(TableMap:: TYPE_PHPNAME, true, [], true);
    

    Edit: As @DarkBee noted in the comments you where already calling the toArray with the correct parameters, so why you aren't getting the related objects in your array representation? The answer probably resides in this step of your code

    $object->setStatusId(5)
    

    You're actually updating your object without updating the database related record, by doing this Propel isn't able to retrieve the correct related object, to avoid this behavior you can set the full Status object instead of the sole id, eg:

    $object->setStatus(StatusQuery::create()->findPk(5));
    

    By doing this you will have the correct Status object information in your array representation, be careful that this (as already said), probably, doesn't reflect the database state.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值