PHP code example of davewid / cactus

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

    

davewid / cactus example snippets

 php


class UserMapper extends \Cactus\Mapper
{
	/**
	 * Use the init() function so you don't have to mess with handling the
	 * adapter injection in the constructor.
	 */
	protected function init()
	{
		$this->table = "user";
		$this->primary_key = "user_id";

		$this->columns = array(
			'user_id' => 'integer',
			'email' => 'string',
			'password' => 'string',
			'first_name' => 'string',
			'last_name' => 'string',
			'status' => 'integer'
			'create_date' => 'dateTime'
		);
	}
}
 php



$pdo = new PDO($dsn, $username, $password);
$adapter = new \Cactus\Adapter\PDO($pdo);

$mapper = new UserMapper($adapter);
 php


class User extends \Cactus\Entity{}
 php


// Find the user with a user_id of 1
$user = $mapper->get(1);

// Get all users
$users = $mapper->all();

// Find user who's email is [email protected]
$found = $mapper->find(array(
	'email' => "[email protected]"
));
 php


// New user assuming $post is posted form data
$user = new User($post);
$mapper->save($user);

// Existing user, after modifications
$user = $mapper->get(1);
$user->first_name = "Billy";
$mapper->save($user);

echo $user->first_name; // Output: Billy
 php


$user = $mapper->get(1);
$mapper->delete($user);
 php


$this->columns = array(
	'user_id' => 'integer',
	'email' => 'string',
	'password' => 'string',
	'first_name' => 'string',
	'last_name' => 'string',
	'status' => 'integer'
	'create_date' => 'dateTime'
);