Introducci贸n a desarrollo web con PHP y Yii
驴Qu茅 es Yii2?
驴Por qu茅 usar PHP y Yii2?
Arquitectura modelo鈥搗ista鈥揷ontrolador
Instalaci贸n de Yii2
Estructura de una aplicaci贸n con Yii2
Configuraci贸n de variables de entorno
Quiz: Introducci贸n a desarrollo web con PHP y Yii
Esquema de la aplicaci贸n
Proyecto: The Book Club
Creaci贸n del schema de la base de datos
Creaci贸n de tablas clubs y club_members
Quiz: Esquema de la aplicaci贸n
Modelos, vistas y controladores
Commands
Model
ActiveRecord
ActiveRecord de la clase author
Relational data: hasOne()
Relational data: hasMany()
Web Controllers
Controller de la clase author
Enrutamiento y creaci贸n de URLS con urlManager
Views
Configuraci贸n de vistas con Smarty
Quiz: Modelos, vistas y controladores
Manejo de usuarios con Yii2
Clase user
Autenticaci贸n de usuarios
Autenticaci贸n de usuarios: LoginForm
User input
Formulario de login con ActiveForm
Validaci贸n de modelo con core validators
Core validators: unique, filter, email
Quiz: Manejo de usuarios con Yii2
Enlaces, navegaci贸n y tablas intermedias
Dar de alta un libro
Asignaci贸n de libros a usuario
Asignaci贸n de libros a usuario: vista
Calificar un libro
Promedio de calificaciones de libro
Relaciones entre tablas intermedias con viaTable()
Enlaces y navegaci贸n
Quiz: Enlaces, navegaci贸n y tablas intermedias
Despliegue y cierre del proyecto
Despliegue de aplicaci贸n de Yii2 a Heroku
You don't have access to this class
Keep learning! Join and start boosting your career
In the world of web development, managing relationships between entities in a database is crucial. One of the most robust PHP frameworks, Yii, makes this possible in an extremely simple and efficient way by implementing ActiveRecord. In this guide, we'll explore how to manage relationships between entities using ActiveRecord in Yii.
ActiveRecord is a design pattern that allows access to data in a database through objects. In Yii, each table in the database is associated with a class, and each row in that table is represented as an object of that class. This makes it easier to manipulate data, as you work directly with objects instead of abstract SQL queries.
To define relationships between models in Yii, we use two magic methods: hasOne
and hasMany
. These functions allow you to specify how models are connected to each other by foreign keys or other criteria.
hasOne
Let's see how to define a relationship in which a book has a single author:
public function getAuthor(){ return $this->hasOne(Author::class, ['authorId' => 'authorId']);}
Here, we are specifying that a book belongs to an author, indicating that the authorId
of the author must match the authorId
of the book. Just like that, a one-to-one relationship is established.
Once the relationship is established, we can easily retrieve related data without the need to write complex queries. Let's consider the implementation of a function to get details of a book and its author:
public function actionBook($bookId){ $book = Book::findOne($bookId);
if (empty($book)) { return 'Book does not exist'; }
printf("%s, by %s", $book->toString(), $book->getAuthor()->name);}
In this code, we search for a book by its bookId
. If the book exists, we print its information and that of the associated author. The getAuthor()
function returns an Author
object, from which we can get attributes such as name
.
Yii stands out in database management because of its ability to simplify common operations. Through ActiveRecord, the need to manually write complicated SQL queries such as joins is minimized, which significantly reduces development time and the possibility of errors.
By implementing relationships such as hasOne
, Yii allows us to focus on business logic without worrying about the complexity of the underlying databases. This, coupled with its efficient performance and active community, makes it an ideal choice for developers looking for efficiency.
We call on all those curious and passionate about web development to continue exploring the possibilities that Yii has to offer. The ease of use and power of its tools will transform the way you interact with databases, taking your projects to the next level.
Contributions 3
Questions 0
Want to see more contributions, questions and answers from the community?