Skip to content

Commit

Permalink
Non-beans in lists wont cause export to fail anymore. Close #408.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor de Mooij committed Jan 2, 2015
1 parent 6e43347 commit db8c69b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions RedBeanPHP/OODBBean.php
Expand Up @@ -434,6 +434,7 @@ public function export( $meta = FALSE, $parents = FALSE, $onlyMe = FALSE, $filte
$vn = array();

foreach ( $value as $i => $b ) {
if ( !( $b instanceof OODBBean ) ) continue;
$vn[] = $b->export( $meta, FALSE, FALSE, $filters );
$value = $vn;
}
Expand Down
79 changes: 79 additions & 0 deletions testing/RedUNIT/Base/Issue408.php
@@ -0,0 +1,79 @@
<?php

namespace RedUNIT\Base;

use RedUNIT\Base as Base;
use RedBeanPHP\Facade as R;

/**
* Issue 408
*
* @file RedUNIT/Mysql/Issue408.php
* @desc Test whether we can export beans with arrays in properties
* (deserialized/serialized on open/update).
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Issue408 extends Base
{

/**
* In the past it was not possible to export beans
* like 'feed' (Model_Feed).
*
* @return void
*/
public function testExportIssue()
{
R::nuke();
$feed = R::dispense( 'feed' );
$feed->post = array(
'first',
'second'
);
R::store( $feed );
$rows = R::getAll('SELECT * FROM feed');
asrt( $rows[0]['post'], '["first","second"]' );
$feed = $feed->fresh();
asrt( is_array( $feed->post ), TRUE );
asrt( $feed->post[0], 'first' );
asrt( $feed->post[1], 'second' );
R::store( $feed );
$rows = R::getAll('SELECT * FROM feed');
asrt( $rows[0]['post'], '["first","second"]' );
$feed = R::load( 'feed', $feed->id );
$feed->post[] = 'third';
R::store( $feed );
$rows = R::getAll('SELECT * FROM feed');
asrt( $rows[0]['post'], '["first","second","third"]' );
$feed = $feed->fresh();
asrt( is_array( $feed->post ), TRUE );
asrt( $feed->post[0], 'first' );
asrt( $feed->post[1], 'second' );
asrt( $feed->post[2], 'third' );
//now the catch: can we use export?
//PHP Fatal error: Call to a member function export() on a non-object
$feeds = R::exportAll( R::find( 'feed' ) );
asrt( is_array( $feeds ), TRUE );
$feed = reset( $feeds );
asrt( $feed['post'][0], 'first' );
asrt( $feed['post'][1], 'second' );
asrt( $feed['post'][2], 'third' );
//can we also dup()?
$feedOne = R::findOne( 'feed' );
R::store( R::dup( $feedOne ) );
asrt( R::count( 'feed' ), 2 );
//can we delete?
R::trash( $feedOne );
asrt( R::count( 'feed' ), 1 );
$feedTwo = R::findOne( 'feed' );
$feed = $feedTwo->export();
asrt( $feed['post'][0], 'first' );
asrt( $feed['post'][1], 'second' );
asrt( $feed['post'][2], 'third' );
}
}
1 change: 1 addition & 0 deletions testing/cli/runtests.php
Expand Up @@ -127,6 +127,7 @@ function activate_driver( $d )
'Blackhole/Plugins',
'Blackhole/Debug',
'Base/Dispense',
'Base/Issue408',
'Base/Threeway',
'Base/Chill',
'Base/Arrays',
Expand Down
11 changes: 11 additions & 0 deletions testing/helpers/classes.php
Expand Up @@ -412,6 +412,17 @@ public function delete() {
}
}

class Model_Feed extends \RedbeanPHP\SimpleModel {
public function update() {
$this->bean->post = json_encode($this->bean->post);
}

public function open() {
$this->bean->post = json_decode($this->bean->post, true);
}
}


/**
* UUID QueryWriter for MySQL for testing purposes.
*/
Expand Down

0 comments on commit db8c69b

Please sign in to comment.