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 today's world, it is not enough to have data stored in databases; it is crucial to transform it into useful information, especially when working with entities such as books, ratings and users. This process may seem complex, but with the right tools and a little ingenuity, it can be done effectively.
The key is to associate data with each other, creating a layer of information that allows you to obtain valuable analysis. For example, you can calculate the total score of a book, the average number of ratings a user has given to his or her readings and, in the case of an author, understand what the average score of his or her books is.
When working with databases, we often need to use intermediate tables to manage relationships. In the example of books and users, the intermediate table user_books
is crucial. To build these relationships, we use simple Query Builder
commands, such as via table
, which eventually translates into a join
in SQL.
via table
in SQL:public function getVotes() { return $this->hasMany( BookScore::class, where('book_id', $this->book_id))->all();}
The goal is to display the average rating for each book, which gives a clear view of how users evaluate it. The following illustrates how to perform this calculation within a model:
public function getScore(): float { $sum = 0; $i = 0;
foreach ($this->votes as $vote) { $i++; $sum += $vote->score; }
return ($i === 0) ? 0 : $sum / $i;}
To provide a better user experience, it is necessary to report both the average rating and the number of votes a book has received. This can be achieved in an attractive way by using a clear and readable notation.
It is not only interesting to know the average of a book, but also to understand the trend of individual ratings of each user. This allows you to analyze how demanding a reader is and how he/she interacts with different works.
public function getVotesAverage(): string { $sum = 0; $i = 0;
foreach ($this->votes as $vote) { $i++; $sum += $vote->score; }
return ($i === 0) ? 'No votes' : sprintf("%.2f", $sum / $i) . " of $i votes";}
One of the key principles when working with data and information is to maintain simplicity and modularity in the code. It is more efficient to first get the data and then perform necessary operations on it, rather than redoing the process.
This way, you not only get valuable information, but also keep the code clean and scalable for future needs. With these hands-on exercises, you can see how easy it is to shell and use information thanks to well-defined functions. Continue exploring this fascinating area of data development and management!
Contributions 1
Questions 0
Want to see more contributions, questions and answers from the community?