Skip to main content

Masa Brooding Ayam

Configure Propel 2.* In Laravel 4.*

Configure Propel In Laravel

Prep Work: Download and install Composer Simply follow the installation instructions found here. Composer is a cool command-line tool that leverages a JSON config file to manage your project dependencies with ease. We will use it in this tutorial to install both Laravel and Propel.
  1. Step 1: Download and install Laravel Once you have Composer, installing Laravel is as simple as executing the following in your command line (e.g., Terminal):
    composer create-project laravel/laravel --prefer-dist
    Laravel will be installed by default in a new folder called “laravel” in the current working directory. To specify a directory for installation other than “laravel”, simply add an argument before the —prefer-dist flag. For example, use “composer create-project laravel/laravel ./newapp/ –prefer-dist” to install Laravel in a new directory called “newapp” in the present working directory.
  2. Step 2: Add Propel 2 to your composer.json file The composer.json file is located in the root directory of your Laravel project. It tells Composer what dependencies the current project has. Open it up in your preferred text editor or IDE and add a line for Propel like so:
    "require": {
    "laravel/framework": "4.1.*",
    "propel/propel": "2.0.*@dev"
    },
    Note that Propel 2.0.* is still in “dev”, so the @dev stability flag must be added to allow it to be loaded. As an alternative, the minimum-stability property can be set to “dev”.
  3. Step 3: Install Propel using Composer Once composer.json has been modified, install Propel by running the following command in your Laravel install directory:
    $ composer update
    Propel will be installed in /path/to/laravel/vendor/bin/propel/. You can test that Propel was properly installed by running the following (again, in your Laravel install directory):
    $ vendor/bin/propel
  4. Step 4: Create a Propel configuration directory Execute the following command from your Laravel install directory to create a new configuration folder for Propel:
    $ mkdir app/config/propel
    Configuration files for Propel can technically live anywhere, so long as they are properly reference in step 8. The application configuration folder is a natural place to put it, however.
  5. Step 5: Copy, write, or reverse engineer your Propel schema.xml file Propel uses an XML file to define the structure and interrelationships of your project’s data models. These are somewhat inextricably tied to the underlying tables and columns of your database implementation – one weakness of the Active Record pattern – but it does mean that updating your database is as easy as updating your schema.xml and running the Propel diff and migrate tasks. The schema.xml is also used to build the PHP model classes that you will use to interface with your data models (more on that in step 6). Essentially, everything in Propel starts with the schema.xml.
    • If you have an existing schema.xml from previous work with Propel, simply copy it into the Propel configuration directory you created in the previous step.
    • If you are starting from scratch and don’t have an existing database or Propel schema.xml, write one to your needs per the schema documentation from Propel.
    • If you have an existing database that you’d like to use, Propel can reverse engineer a schema.xml and save you a bunch of time. Simply run the following command in /path/to/laravel/app/config/propel/, replacing [VAR_NAME] with the relevant values:
      ../../../vendor/bin/propel reverse "[DB_ADAPTER – e.g., mysql]:host=[HOST];port=[PORT];dbname=[DB_NAME];user=[DB_USER];password=[DB_PASS]" --input-dir="." --output-dir="." --database-name="[DB_NAME]"
      example code
      ../../../vendor/bin/propel reverse "mysql:host=localhost;port=3306;dbname=nufaza_sim;user=root;password=root"
      Note that the reverse-engineered schema.xml may be somewhat limited based on what information your database encodes. For example, if you are using SQLite – a typeless database – the column elements won’t have defined types. Or if your database doesn’t have foreign key relationships (often the case with the MySQL-default MyISAM storage engine), you may have to define them explicitly after the schema.xml has been generated.
  6. Step 6: Generate your Propel PHP model classes
    • edit in schema.xml <database name="default" to <database name="[NAME_DATABASE]"
    • edit in schema.xml add namespace to <database namespace="propel/" 
    • copy scema.xml from generated-reversed-database to ../
    • add file propel.xml
    • <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
      <config>
      <propel>
      <database>
          <connections>
              <connection id="nufaza_sim">
                  <adapter>mysql</adapter>
                  <classname>Propel\Runtime\Connection\ConnectionWrapper</classname>
                  <dsn>mysql:host=localhost;dbname=nufaza_sim</dsn>
                  <user>root</user>
                  <password>root</password>
              </connection>
          </connections>
      </database>
      <runtime>
          <defaultConnection>nufaza_sim</defaultConnection>
          <connection>nufaza_sim</connection>
      </runtime>
      <generator>
          <defaultConnection>nufaza_sim</defaultConnection>
          <connection>nufaza_sim</connection>
      </generator>
      </propel>
      </config>
      Run the following command in /path/to/laravel/app/config/propel/ to generate your PHP model classes based on the schema.xml.
      $ ../../../vendor/bin/propel model:build --mysql-engine="InnoDB" --output-dir="../../models" 
      example command
      ../../../vendor/bin/propel model:build --mysql-engine="InnoDB" --output-dir="../../models" 
      This will create a ton of PHP classes which Propel uses to handle its ORM goodness. We’ve specified an output directory using the –output-dir flag, which will export all of the PHP classes into a “propel” directory within the “models” folder of the Laravel application. Check out the Propel documentation for a more detailed description of the classes generated.
  7. Step 7: Craft your Propel runtime configuration You can either define your runtime configuration directly in PHP, or write an XML configuration and compile that into PHP. We’ll describe the latter option here. Below is an template runtime-conf.xml, which should go into your Propel configuration folder (/path/to/laravel/app/config/propel/).
    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    <propel>
    <datasources default="[DB_NAME]">
    <datasource id="[DB_NAME]">
    <adapter>[DB_ADAPTER]</adapter>
    <!-- e.g., "sqlite", "mysql", "myssql", or "pgsql" -->
    <connection>
    <dsn>[DB_ADAPTER]:host=[HOST];port=[PORT];dbname=[DB_NAME];user=[DB_USER];password=[DB_PASS]</dsn>
    <user>[DB_USER]</user>
    <password>[DB_PASS]</password>
    </connection>
    </datasource>
    </datasources>
    <log>
    <logger name="defaultLogger">
    <type>stream</type>
    <path>../app/config/propel/propel.log</path>
    <level>300</level>
    </logger>
    </log>
    </propel>
    </config>
    Note that the id attribute of the element in runtime-conf.xml must correspond to the database name supplied in the schema.xml back in step 5. add propel.xml in folder (/path/to/laravel/app/config/propel/).
    <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
    <config>
    <propel>
        <database>
            <connections>
                <connection id="[databas_name]">
                    <adapter>mysql</adapter>
                    <classname>Propel\Runtime\Connection\ConnectionWrapper</classname>
                    <dsn>mysql:host=localhost;dbname=[databas_name]</dsn>
                    <user>root</user>
                    <password>root</password>
                </connection>
            </connections>
        </database>
        <runtime>
            <defaultConnection>[databas_name]</defaultConnection>
            <connection>[databas_name]</connection>
        </runtime>
        <generator>
            <defaultConnection>[databas_name]</defaultConnection>
            <connection>[databas_name]</connection>
        </generator>
    </propel>
    </config>
    Once you have saved the runtime-conf.xml, run the following command in /path/to/laravel/app/config/propel/ to compile your config.php file into the same directory:
    ../../../vendor/bin/propel config:convert --input-dir="." --output-dir="." --output-file="config.php" -vvv
    example comand
    ../../../vendor/bin/propel config:convert --input-dir="." --output-dir="." --output-file="config.php" -vvv
  8. Step 8: Add Propel to your Laravel startup To ensure Propel is loaded in during Laravel bootstrapping, simply require the config file you just generated by adding the following line to /path/to/laravel/app/start/global.php, just prior to requiring filters (near the bottom):
    // setup Propel
    require_once app_path().'/config/propel/config.php';
  9. Step 9: Clear the Autoload Finally, run the following in your Laravel install directory to recompile autoload to recognize the newly added models:
    $ php artisan dump-autoload
    That’s all, folks! And with that, Propel’s ORM classes and capability should be available to use within your Laravel implementation. To access the Propel class itself, simply use the following namespaced identifier:
    \Propel\Runtime\Propel
    test me
    $absen = new \Propel\Penugasan();
    // $absen = \Propel\PenugasanQuery::create()->find();
    // $absen = new Nufazasim\ChildUser();
    echo "<pre>";
    print_r($absen);
    echo "</pre>";

Comments

Popular posts from this blog

Pengertian DataBase, MySQL dan phpMyAdmin

Pengertian DataBase, MySQL dan phpMyAdmin Pengertian DataBase DataBase adalah kumpulan informasi yang disusun berdasarkan cara tertentu dan merupakan suatu kesatuan yang utuh. Dengan sistem tersebut data yang terhimpun dalam suatu database dapat menghasilkan informasi yang berguna. Asal Mula Istilah Database Istilah “Database” berawal dari ilmu komputer. Meskipun kemudian artinya semakin luas, memasukkan hal-hal di luar bidang elektronika, postingan saya kali ini mengenai database komputer. Catatan yang mirip dengan database sebenarnya sudah ada sebelum revolusi industri yaitu dalam bentuk buku besar, kuitansi dan sekumpulan data yang berhubungan dengan bisnis. Konsep Dasar Database Konsep dasar dari database adalah kumpulan dari catatan-catatan, atau potongan dari pengetahuan. Sebuah database memiliki penjelasan terstruktur dari jenis fakta yang tersimpan di dalamnya: penjelasan ini disebut skema. Skema menggambarkan obyek yang diwakili suatu database, dan hubungan di antar...

Contoh aplikasi konversi Suhu java console (cmd) If Else

Contoh aplikasi konversi Suhu java console (cmd) If Else package tugas; import java.util.Scanner; public class KonversiSuhu { public static void main (String[] args){ int menu; double Suhuawal, Suhu; System.out.println("Aplikasi Konversi Suhu"); System.out.println("Menu Aplikasi"); System.out.println("[1] Dari Celcius ke Reamur"); System.out.println("[2] Dari Celcius ke Farenheit"); System.out.println("[3] Dari Reamur ke Celcius"); System.out.println("[4] Dari Reamur ke Farenheit"); System.out.println("[5] Dari Farenheit ke Celcius"); System.out.println("[6] Dari Farenheit ke Reamur"); System.out.print("Masukan Kode menu : "); Scanner Scanner = new Scanner(System.in); menu = Scanner.nextInt(); if(menu == 1){ System.out.println("Konversi Dari Celcius ke Reamur"); System.out.print("Inputkan Nilai Suhu Celcius: "); Suhuawal = Sca...

Menambahkan custom connection in symfony 4 (multiple connection)

add ini service.yml app.event.authentication_success_listener : class : App\EventListener\AuthenticationSuccessListener tags : - { name : kernel.event_listener, event : lexik_jwt_authentication.on_authentication_success, method : onAuthenticationSuccessResponse } # abc def ghi # ------------ app.date_normalizer : class : Symfony\Component\Serializer\Normalizer\DateTimeNormalizer arguments : - { 'datetime_format' : 'Y-m-d H:i:s' } and create file doctrine.yml parameters : # Adds a fallback DATABASE_URL if the env var is not set. # This allows you to run cache:warmup even if your # environment variables are not available yet. # You should not need to change this value. env(DATABASE_URL) : '' doctrine : dbal : default_connection : default connections : default : driver : 'pdo_sqlsrv' server_version : '11.0' url : ...