Skip to content
Tommy Teasdale edited this page Sep 10, 2015 · 17 revisions

A factory is a way to find and instantiate one or many entities from the database. It accesses to database to fetch corresponding data and build entities. The entity instanciation is directly lent to the entities so the factory only include the required methods to find and launch instanciations.

This feature requires prior knowledge of the Entity Model and Database Access

Factory Interface

The ApineEntityFactory class on its own can't create anything. It's an abstract class containning methods to be implemented by concrete factories. Because it is a abstract class, the concrete factory must extend from ApineEntityFactory.

Concrete entities must implement these three methods :

Boolean ApineEntityFactory::is_exists_id(string $a_id)

Verify if an entry has the provided ID value

ApineEntityModel ApineEntityFactory::create_by_id(string $a_id)

Return an entity instantiated from the provided ID

Liste<ApineEntityModel> ApineEntityFactory::create_all()

Return a collection of instantiated every entries

Sample Class

class SampleFactory extends ApineEntityFactory {

    public static function is_id_exist ($entity_id) {

        $id_sql = (new Database());
        $id_sql = $database->select("SELECT `id` FROM `apine_entities` WHERE `id`=$entity_id");
	
        if ($id_sql) {
            return true;
        }
	
        return false;

    }

    public static function create_all () {

        $request = (new Database());
        $request = $database->select('SELECT `id` from `apine_entities`');
        $liste = new Liste();
	
        if ($request != null && count($request) > 0) {
            foreach ($request as $item) {
                $liste->add_item(new ApineEntity('apine_entities', $item['id']));
            }
        }
	
        return $liste;

    }

    public static function create_by_id ($a_id) {
	
        $database=new Database();
        $sql_id = $database->prepare('SELECT `id` FROM `apine_entities` WHERE `id`=?');
        $ar_sql = $database->execute(array(
                    $a_id
        ), $sql_id);
	
        if ($ar_sql) {
            $return = new ApineEntities('apine_entities', $ar_sql[0]['id']);
        } else {
            $return = null;
        }
	
        return $return;

    }
}
Clone this wiki locally