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 context of a book management application, it is essential to be able to determine whether a user has a specific book. In the previous exercise, the goal was to modify the view to reflect whether a user already has a book or not. This information is crucial to customize the user's interaction with the platform, offering personalized options such as rating the book or removing it from their list.
To implement this functionality, we start from the view, where we will add a conditional that shows different options depending on the status of the book:
{if (G\AppUserIdentity::hasBook($bookId)) { // Logic for when the user has the book} else { // Logic for when the user doesn't have the book}
This code snippet evaluates whether the user has registered the book as theirs and, based on that, deploys the corresponding actions.
hasBook
function in the user model?The hasBook
function is key to determine the ownership of a book in the user model. It checks if the specific book is associated with the user by querying the database.
public function hasBook($bookId) { $userBooks = UserBook::where([ 'user_id' => $this->id, 'book_id' => $bookId ])->get();
return !empty($userBooks);}
In this code:
UserBook
table looking for user and book matches.where
method filters the records that match the user_id
and book_id
.true
. Otherwise, false
is returned.Once it is confirmed that the user owns a book, two options are offered in the view: to rate the book or to indicate that the user no longer owns it. The rating is done via a form that sends the score to the BookScore
driver.
As part of the future implementation, a form will be designed for users to rate their books. This form will send a rating from 1 to 5 to the BookScore
controller, taking care of receiving it and processing it properly in the system.
<form action="/book-score" method="post"> <label for="rating">Ratethis book:</label> <select id="rating" name="rating"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <button type="submit">Send</button></form>.
This implementation not only improves the user experience, allowing you to interact and give feedback on your books, but also creates a more dynamic system that can adapt to future functionality, such as book revaluation or rating data analysis.
Keep exploring and extending your knowledge to continue building robust, user-centric applications!
Contributions 1
Questions 0
Want to see more contributions, questions and answers from the community?