PHP code example of sizuhiko / cake_fabricate

1. Go to this page and download the library: Download sizuhiko/cake_fabricate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sizuhiko / cake_fabricate example snippets


use Fabricate\Fabricate;
use CakeFabricate\Adaptor\CakeFabricateAdaptor;

Fabricate::config(function($config) {
    $config->adaptor = new CakeFabricateAdaptor();
});

$results = Fabricate::attributes_for('Posts', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $results is array followings :
array (
  0 => 
  array (
    'title' => 'Lorem ipsum dolor sit amet',
    'body' => 'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida, phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit, feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.',
    'created' => '2013-10-09 12:40:28',
    'updated' => '2013-10-09 12:40:28',
  ),
  1 => 
  array (
  ....

$result = Fabricate::build('Posts', function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

// $result a Model\Entity\Post object.
 ......

Fabricate::create('Posts', 10, function($data){
    return ["created" => "2013-10-09 12:40:28", "updated" => "2013-10-09 12:40:28"];
});

Fabricate::create('Users', function($data, $world) {
    return [
        'user' => 'taro',
        'posts' => $world->association('Posts', 3, ['author_id'=>false]),
    ];
});
// can use defined onbject.
Fabricate::define(['PublishedPost', 'class'=>'Posts'], ['published'=>'1']);
Fabricate::create('Users', function($data, $world) {
    return [
        'user' => 'taro',
        'posts' => $world->association(['PublishedPost', 'association'=>'Posts'], 3, ['author_id'=>false]),
    ];
});
// can use association alias (Post belongs to Author of User class)
Fabricate::define(['PublishedPost', 'class'=>'Posts'], ['published'=>'1']);
Fabricate::create('PublishedPost', 3, function($data, $world) {
    return [
        'author' => $world->association(['Users', 'association'=>'Author'], ['id'=>1,'user'=>'taro']),
    ];
});

Fabricate::config(function($config) {
    $config->adaptor = new CakeFabricateAdaptor([
        CakeFabricateAdaptor::OPTION_FILTER_KEY => true
        CakeFabricateAdaptor::OPTION_VALIDATE   => true
    ]);
});