PHP code example of energylab / gacela

1. Go to this page and download the library: Download energylab/gacela 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/ */

    

energylab / gacela example snippets


class User {

	protected $data = [
		'id' => 1,
		'name' => 'Bobby Mcintire',
		'email' => '[email protected]'
	];

	protected $contents = [];

}

class User {

	protected $data = [
		'id' => 2,
		'name' => 'Frankfurt McGee',
		'email' => '[email protected]',
		'phone' => '9876543214'
	];

	protected $contents = [
        [
            'id' => 1,
            'userId' => 2,
            'title' => 'Beginners Guide to ORMs',
            'content' => 'Read this guide all the way to the end'
        ]
	];

}

class Model_User extends ORM
{

}

$user = ORM::find('User', 1);

// echo's Bobby Mcintire to the screen
echo $user->name;

$user->email = '[email protected]'

$user->save();

$source = \Gacela\Gacela::createDataSource(
    [
        'name' => 'db',
        'type' => 'mysql' // Other types would be mssql, postgres, sqllite
        'host' => 'localhost',
        'user' => 'gacela',
        'password' => '',
        'schema' => 'gacela'
    ]
);

\Gacela\Gacela::instance()->registerDataSource($source);

$source = \Gacela\Gacela::createDataSource(
    [
        'name' => 'salesforce',
        'type' => 'salesforce',
        'soapclient_path' => MODPATH.'sf/vendor/soapclient/',
			/**
			 * Specify the full path to your wsdl file for Salesforce
			 */
		'wsdl_path' => APPPATH.'config/sf.wsdl',
		'username' => '[email protected]',
		'password' => 'SecretPasswordWithSalesforceHash',
		/**
		 * Specifies which Salesforce objects are available for use in Gacela
		 */
         'objects' => []
    ]
);

/*
 * Assuming that you are bootstrapping from the root of your project and that you want to put your 
 * custom application code in an app directory
 */
\Gacela\Gacela::instance()->registerNamespace('App', __DIR__.'/app/');

/*
 * A handy trick if you want to put your Mappers/Models or other custom extensions for Gacela in the global 
 * namespace
 */
\Gacela\Gacela::instance()->registerNamespace('', __DIR__.'/app/');

/*
 * __DIR__.'/app/Mapper/User.php'
 */



namespace Mapper;

class User extends \Gacela\Mapper\Mapper {}
 
/*
 * __DIR__.'/app/Mapper/User.php'
 */



namespace App\Mapper;

use \Gacela\Mapper\Mapper as M;

class User extends M {}

/*
 * __DIR__.'/app/Model/Model.php'
 */


namespace Model;

class Model extends \Gacela\Model\Model {}


$cache = new \Cache;

\Gacela\Gacela::instance()->enableCache($cache);

/*
 * Again assume that we have created an 'app' directory and registered it into the global namespace with Gacela.
 * 
 * As I mentioned before, I prefer to always override the default Model and Mapper classes in my application so 
 * I will that first.
 * app/Mapper/Mapper
 */
 

namespace Mapper;

class Mapper extends \Gacela\Mapper\Mapper {}


/*
 * You can also easily override the \Gacela\Gacela class by creating a shorthand class in the app/ directory
 * that extends \Gacela\Gacela. To simplify calls, I like to create a extended class 'G'. Future examples all
 * assume that this extended class exists.
 */
$user = \G::instance()->find('User', 1);

// echos Bobby Mcintire to the screen
echo $user->name;

$user->email = '[email protected]'

// Saves the updated record to the database
$user->save();

/*
 * The 



namespace Mapper;

class Note extends Mapper {

    protected $_modelName = 'Comment'
}

$bobby = \Gacela\Gacela::instance()->find('User', 1);

/*
 * Outputs Bobby Mcintire
 */
echo $bobby->email;

/*
 * The \Gacela\Criteria object allows users to specify simple rules for filtering, sorting and limiting data without all of the complexity of
 * a full built-in query builder.
 * \Gacela\Gacela::instance()->findAll() returns an instance of \Gacela\Collection\Arr
*/

$criteria = \Gacela\Gacela::criteria()
    ->equals('userId', 1);

\Gacela\Gacela::instance()->findAll('Post', $criteria);



namespace Mapper;

class User extends Mapper
{

	/**
	 * Fetches a Collection of all users with no posts
	*/
	public function findUserWithNoPosts()
	{
		/**
		 * Mapper::_getQuery() returns a Query instance specific to the Mapper's data source.
		 * As such, the methods available for each Query instance will vary.
		*/
		$query = $this->_getQuery()
			->from('users')
			->join(array('p' => 'contents'), "users.id = p.userId, array(), 'left')
			->where('p.published IS NULL');

		/**
		 * For the Database DataSource, returns an instance of PDOStatement.
		 * For all others, returns an array.
		*/
		$data = $this->_runQuery($query)->fetchAll();

		/**
		 * Creates a Gacela\Collection\Collection instance based on the internal type of the data passed to Mapper::_collection()
		 * Currently, two types of Collections are supported, Arr (for arrays) and PDOStatement
		*/
		return $this->_collection($data);
	}
}



namespace Mapper;

use Gacela as G;

class User extends Mapper
{
	public function find($id)
	{
		$criteria = \G::instance()criteria()->equals('id', $id);

		$rs = $this->_runQuery($this->_base($criteria))->fetch();

		if(!$rs)
		{
			$rs = new \stdClass;
		}

		return $this->_load($rs);
	}

	public function findAll(\G\Criteria $criteria = null)
	{
		/**
		 * Returns an instance Gacela\Collection\Statement
		**/
		return $this->_runQuery($this->_base($criteria));
	}

	/**
	 * Allows for a unifying method of fetching the custom data set for find() and find_all()
	**/
	protected function _base(\G\Criteria $criteria = null)
	{
		$attempts = $this->_getQuery()
            ->from('logins', 'attempts' => 'COUNT(*)')
            ->where('logins.userId = users.id');

        $logins = $this->_getQuery()
            ->from('logins', 'logins' => 'COUNT(*)')
            ->where('logins.userId = users.id')
            ->where('succeeded = 1');

        return $this->_getQuery($criteria)
			->from('users', [
                'users.*',
                'attempts' => $attempts,
                'logins' => $logins
            ]);
	}
}

$user = new \Model\User('\Mapper\User');

$user->name = 'Noah Dingermeister';
$user->email = '[email protected]';

if($user->validate()) {
    $user->save();
} else {
    print_r($user->errors);
}



namespace Model;

class User extends Model {

    protected function _getFirstName()
    {
        $first = explode(' ', $this->name);

        return current($first);
    }

    protected function _getLastName()
    {
        $last = explode(' ', $this->name);

        return end($last);
    }
}




namespace Model;

class Post extends Model {

    protected function _setContent($val)
    {
        $val = str_replace(['lame', 'boring', 'stupid'], 'AMAZING', $val);

        /**
         * The _set() method verifies that the new value is distinct from the current value
         * and then moves the old value into the _originalData array, sets the new value into _data
         * and adds the specified key to the _changed array.
         */
        $this->_set('content', $val);
    }
}



namespace Model;

class User extends Model {

    public function validate(array $data = null)
    {
        if($data) {
            $this->setData($data);
        }

        if(strpos($this->email, 'gacela.com') === false) {
            $this->_errors['email'] = 'not_gacela_domain';
        }

        return parent::validate();
    }
}