Category: Yii

  • Data Access Objects

    To execute an SQL query, you should follow these steps − Step 1 − Create a function called actionTestDb in the SiteController. The above example shows various ways of fetching data from a DB. Step 2 − Go to the address http://localhost:8080/index.php?r=site/test-db, you will see the following output. Create an SQL Command To create an SQL command with parameters, you should…

  • Database Access

    Yii DAO (Database Access Object) provides an API for accessing databases. It also serves as the foundation for other database access methods: active record and query builder. Yii DAO supports the following databases − Creating a Database Connection Step 1 − To create a database connection, you need to create an instance of the yii\db\Connection class.…

  • Dependency Injection

    A DI(dependency injection) container is an object that knows how to instantiate and configure objects. Yii provides the DI container via the yii\di\Container class. It supports the following kinds of DI − The DI container supports constructor injection with the help of type hints − Property and setter injections are supported through configurations − In case…

  • Configurations

    Configurations are used to create new objects or initializing the existing ones. Configurations usually include a class name and a list of initial values. They may also include a list of event handlers and behaviors. The following is an example of the database configuration − The Yii::createObject() method takes a configuration array and creates an object based…

  • Creating a Behavior

    Assume we want to create a behavior that will uppercase the “name” property of the component the behavior is attached to. Step 1 − Inside the components folder, create a file called UppercaseBehavior.php with the following code. In the above code we create the UppercaseBehavior, which uppercase the name property when the “beforeValidate” event is triggered. Step 2 − To…

  • Behaviors

    Behaviors are instances of the yii\base\Behavior class. A behavior injects its methods and properties to the component it is attached to. Behaviors can also respond to the events triggered by the component. Step 1 − To define a behavior, extend the yii\base\Behavior class. The above code defines the behavior with one property (prop1) and one method (myFunction). When…

  • Creating Event

    In this chapter we will see to create an event in Yii. To show events in action, we need data. Preparing the DB Step 1 − Create a new database. Database can be prepared in the following two ways. Step 2 − Configure the database connection in the config/db.php file. The following configuration is for the system used currently.…

  • Events

    You can use events to inject custom code at certain execution points. You can attach custom code to an event, and when the event is triggered, the code gets executed. For example, a logger object may trigger a userRegistered event when a new user registers on your web site. If a class needs to trigger events, you should extend…

  • GridView Widget

    The GridView widget takes data from a data provider and presents data in the form of a table. Each row of the table represents a single data item, and a column represents an attribute of the item. Step 1 − Modify the datawidget view this way. Step 2 − Go to http://localhost:8080/index.php?r=site/data-widget, you will see a typical usage of the…

  • ListView Widget

    The ListView widget uses a data provider to display data. Each model is rendered using the specified view file. Step 1 − Modify the actionDataWidget() method this way. In the above code, we create a data provider and pass it to the datawidget view. Step 2 − Modify the datawidget view file this way. We render the ListView widget.…