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.

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

报告相同问题?