Coder Social home page Coder Social logo

doctrine-repository's Introduction

Doctrine Repository

Enables TYPO3 to use External Databases via Doctrine ORM

Setup

1. Database Configuration

Create a configuration for your Cutstom DB AdditionalConfiguration.php we use myDb as a Identifier in the examples below

$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['myDb'] = [
    'charset' => 'utf8',
    'dbname' => 'TYPO3_DATABASE_DBNAME',
    'driver' => 'mysqli',
    'host' => 'TYPO3_DATABASE_HOST',
    'password' => 'TYPO3_DATABASE_PASSWORD',
    'port' => 3306,
    'user' => 'TYPO3_DATABASE_USERNAME',
];

2. Repository Definition

Create an Repository do get Access to the Database Connection by Extending Wolffc\DocrineRepository\Persistence\AbstractDoctrineBasedRepository

see example below:

<?php


namespace MyCompany\MyExtension\Repository;


class MyCustomerRepository extends Wolffc\DocrineRepository\Persistence\AbstractDoctrineBasedRepository
{
    public function getDatabaseIdentifier()
    {
        return 'myDb';
    }
    
   
}

3. Domain Models

Create Domain Models which use Doctrine Annotations. see example Domain Object Below

<?php

namespace MyCompany\MyExtension\Domain\Model;


/**
 * @Entity @Table(name="table_kunden")
 **/
class Customer extends Wolffc\DocrineRepository\DomainObject\AbstractDoctrineDomainObject
{

    /**
     * @var string
     * @Id @Column(type="integer", name="KundenNummer")
     */
    protected $customerNumber;

    /**
     * @var string
     * @Column(type="string", name="Name")
     */
    protected $name;
    

    /**
     * @var MyCompany\MyExtension\Domain\Model\ServiceOffice
     * @ManyToOne(targetEntity="Wolffc\DocrineRepository\Domain\Model\ServiceOffice")
     * @JoinColumn(name="Filiale", referencedColumnName="ID")
     */
    protected $serviceOffice;

 // ... define getters and Setters for Properties as usual ... 
    

    

4. Repository usage in Controllers

Start Using the Repository in Your Controller as you would do with any Extbase Repository

<?php

    namespace MyCompany\MyExtension\Controller;
    
    use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
    use MyCompany\MyExtension\Repository\MyCustomerRepository;

    class CustomerController extends ActionController
    {
        /**
         * @var \MyCompany\MyExtension\Repository\MyCustomerRepository
         * */
        protected $customerRepository;
        
        public function listAction() {
            $customers = $this->customersRepository->findAll();
        }

Form ViewHelper

the Extension Provides an Form ViewHelper which is Able to handle Doctrine Based Object. inside this form viewHelper you can use the regular Extbase/Fluid Form viewHelpers See an Example Below:

{namespace doctrine=Wolffc\DocrineRepository\ViewHelpers}

<div>
     <doctrine:form action="updateCustomer" object="{customer}" name="customer">
        <label>Name: <f:form.textfield property="name" /></label>
        <f:form.submit value="Submit"></f:form.submit> 
     </doctrine:form>
</div>

IMPORTANT NOTES: Persistance with Doctrine

currently if your call persistAll() method on a Repository all Objects Using the DoctrineRepositoy will be Persisted as this function calls $this->entityManager->flush(); not only Objects in this Repository!

if you are not careful this might lead to unintended Persistance of Objects you do not want to persist / modify.

doctrine-repository's People

Contributors

wolffc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.