gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 279 forks source link

Bulk property assignment #699

Closed benmajor closed 5 years ago

benmajor commented 5 years ago

Is there any chance of adding something like the following to the roadmap? It would certainly be helpful in lots of instances (not least of which is importing existing data or CSVs, or similar):

$book = R::dispense('book', [
    'name'   => 'The Jabberwocky',
    'author' => 'Carroll, Lewis',
    'year'   => 2009
]);
Lynesth commented 5 years ago

Hey there,

How is that different from R::import or this ?

$book = R::dispense( [
        '_type' => 'book',
        'title'  => 'Gifted Programmers',
        'author' => [ '_type' => 'author', 'name' => 'Xavier' ],
        'ownPageList' => [ ['_type'=>'page', 'text' => '...'] ]
    ] );
benmajor commented 5 years ago

Hmm, good point!

Lynesth commented 5 years ago

@benmajor Just to add that you could also use R::convertToBeans with your own array (instead of database's rows)

    /**
     * Converts a series of rows to beans.
     * This method converts a series of rows to beans.
     * The type of the desired output beans can be specified in the
     * first parameter. The second parameter is meant for the database
     * result rows.
     *
     * Usage:
     *
     * <code>
     * $rows = R::getAll( 'SELECT * FROM ...' )
     * $beans = R::convertToBeans( $rows );
     * </code>
     *
     * As of version 4.3.2 you can specify a meta-mask.
     * Data from columns with names starting with the value specified in the mask
     * will be transferred to the meta section of a bean (under data.bundle).
     *
     * <code>
     * $rows = R::getAll( 'SELECT FROM... COUNT(*) AS extra_count ...' );
     * $beans = R::convertToBeans( $rows );
     * $bean = reset( $beans );
     * $data = $bean->getMeta( 'data.bundle' );
     * $extra_count = $data['extra_count'];
     * </code>
     *
     * New in 4.3.2: meta mask. The meta mask is a special mask to send
     * data from raw result rows to the meta store of the bean. This is
     * useful for bundling additional information with custom queries.
     * Values of every column whos name starts with $mask will be
     * transferred to the meta section of the bean under key 'data.bundle'.
     *
     * @param string $type     type of beans to produce
     * @param array  $rows     must contain an array of array
     * @param string $metamask meta mask to apply (optional)
     *
     * @return array
     */
    public static function convertToBeans( $type, $rows, $metamask = NULL )