Web progamming and database development

Diary of problems and solutions related to web programming, database development and related technologies and platforms.

Generating CRUD based on Doctrine Entity using Symfony2

In MySql database 

Initially make sure you have your database created on the mysql.

Create a table product on that database and create the fields for the product table.

In Git Bash

We assume that you are in your project root directory

Run the following command to create crud controller

php app/console generate:doctrine:crud

This will help you list the records, add records, edit records and delete records.

Entity shortcut name: AcmeDemoBundle:Product
If you want to create write action (means if you want to create/edit/delete record), then you have enter yes
Configuration format: yml
Routes prefix [/product] : Press Enter to give the routing prefix as it is.
Do you confirm generation: Press Enter to confirm.
Confirm automatic update of routing? Press Enter to confirm.

This will create CRUD controller.

Once a controller is created, if you want to overwrite any of the things to the existing controller, then run the following command.

php app/console generate:doctrine:crud --overwrite

No comments :

Post a Comment

Creating and removing Doctrine Entity using Symfony2 (Creating CRUD Application)

Creating Doctrine Entity

Open up the command line Git Bash

Go to the project folder
$ cd C:/wamp/www/Symfony2Project

Run the following command to create doctrine entity
php app/console generate:doctrine:entity

This entity helps to create class properties, getters and setters functions.

Entity shortcut name: AcmeDemoBundle:Product
Configuration format: yml

New field name: title
Field type (string): Enter
Field length [255]:100

New field name: price
Field type (string): float

New field name: (If you do not want to add extra field names, then press Enter)
Do you want to generate an empty repository class [no]? Enter
Do you want to confirm generation [yes]? Enter 

Removing Entity

- Delete Product.php file inside src/Acme/DemoBundle/Entity folders
- Delete Product.orm.yml file inside src/Acme/DemoBundle/Resources/config/doctrine folders

Reference: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_entity.html

No comments :

Post a Comment

Creating and deleting bundle using Symfony2

Creating Bundle

Open up the command line Git Bash

Go to the project folder
$ cd C:/wamp/www/Symfony2Project

Run the generate bundle command
$php app/console generate:bundle

Bundle namespace: Suresh/Bundle/CrudBundle
Press Enter to confirm namespace
Press Enter again to confirm the target directory src.
Configuration format: yml
Generate the whole directory structure? yes
Do you confirm generation? Press enter to confirm.
Update kernel? Press enter for yes
Update routing? Press enter for yes

Now the bundle has been created and code has generated. This will also create a DefaultController.php inside Controller.

Running web server
$ php app/console server:run

To access the page, see the routing.yml inside Suresh/Bundle/CrudBundle/Resources/config folders. You will see the routing style as /hello/{name}

Open a browser and run the page like this way:

http://localhost:8000/hello/John

Deleting Bundle

To delete bundle, follow these steps
- Delete the folder "Suresh" located inside src folder.
- Open AppKernel.php located inside app folder. Inside registerBundles() function delete the following line.
new Suresh\Bundle\CrudBundle\SureshCrudBundle(),
- Open routing.yml located inside app/config and delete the following lines.
suresh_crud:
    resource: "@SureshCrudBundle/Resources/config/routing.yml"
    prefix:   /

This will help you deleting the bundle completely.

No comments :

Post a Comment

Step to follow while installing Symfony2

Follow these steps to install Symfony2 on your machine. In case if there is difficulty, follow this installation guideline from youtube

1. a. Download Symfony2 from http://symfony.com/download. Unzip it and paste it to the folder inside www if you are using WAMP. Lets say you put everything inside Symfony2Test.
    b. Run WampServer and browse the Symfony2 Package via localhost and go to Symfony2/web/config.php
    c. There you need to set up things and configure the website.

This step 1 is a test step to make sure that you have set up necessary things to run Symfony.

2. Download GIT for windows (if you are using windows) from http://git-scm.com/download/win. Install this GIT on your machine.

Note: To use GIT properly you need to make sure that there is path defined on Environment Variable. If not add this statement on the PATH variable "C:\wamp\bin\php\php5.5.12".

3. Open GIT Bash and
a. Run the following statement to get install Composer via GIT Bash (command prompt) mode. Composer is a package manager which will later help you to install Symfony2 on your machine at later stage.

curl -sS https://getcomposer.org/installer | php

This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory.

For details about installing the visit this page. When you install composer, it installs the composer path to the system by itself.

b. Run the following command to create Symfony Project on your machine.

The general command to install Composer is:

$ composer create-project symfony/framework-standard-edition path/

In our case, you have to specify the correct path. Let say you want to create a new Symfony project called "Symfony2Project". Then run like this

$ composer create-project symfony/framework-standard-edition C:/wamp/www/Symfony2Project 

4. Creating and storing Symfony project in git. For details, check this link
a. Go to your project folder.
$ cd C:/wamp/www/Symfony2Project

b. Initialize your git repository
$ git init

c. Add all of initial files to git
$ git add .

d. Create an initial commit with your started project
$ git commit -m "Initial commit"

In case if you experience the problem like "git: fatal unable to auto-detect email address" then in that case, run the following statements on GIT Bash
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
With this, you will have fully functional Symfony project which is committed to git. Now you can start developing the project. Side by side you can commit the changes to git repository.

Setting up Project Using Relative Path

The above steps will help you install Symfony2 using absolute path (C:/wamp/www/Symfony2Project). If you want to install Symfony2 using relative path, then amend the steps like this way for following points.

Initially go the current root directory
$ cd C:/wamp/www

3b. Run the following command to create Symfony Project on your machine.

$ composer create-project symfony/framework-standard-edition ./Symfony2Project

No comments :

Post a Comment