Using Symfony to override a set function in an Entity with the id of a different Entity

Is it possible to modify a set function of an entity based on a field from another entity?

I have a field in table 1 (column E) that could contain either the value from column C or D (although having two fields with the same information seems messy, I am trying to make it work for now). The value in field E depends on another table (2).

For example: If in table 2, either "crit1" or "crit2" are "localisation", then in table 1, column "tri_id" (column F) will take the value from column D, otherwise it will take the value from column E.

Here is an example of the structure of these tables:

=> "adapte_gaucher" means "left-handed adapted"

https://i.stack.imgur.com/rzXZH.png

This is what I was attempting in my entity:

/**
 * @param int $triId
 */
public function setTriId(int $shopId, int $triId): void
{
    $entityManager = $this->getDoctrine()->getManager();
    $products = $this->getDoctrine()
        ->getRepository(Table1::class)
        ->find($shopId)
 
    if(Table1->crit1 != "localisation" || Table1->crit2 != "localisation" ) {
        $triId = $this->tri21 ;
}   else {
    $triId = $this->tri12 ;
}
    $this->triId = $triId;
    $entityManager->persist($triId);
    $entityManager->flush();
}

Would this be a suitable solution? As a beginner, I'm unsure if this is a good approach or if I should avoid it altogether. This project is part of my internship and I only have one year of programming experience without any framework training. Apologies for the dramatic plea, but your guidance would greatly assist me in succeeding.

Thank you in advance.

Answer №1

Despite the unconventional notion of using an "IF", I am intrigued to experiment with a simple concatenation approach:

public function setTri12()
{
    $this->tri12 = $shop_id . $pack_id . $adapte_gaucher_id;
    return $this;
}

Alternatively:

public function setTri12()
{
    $this->tri12 = $this->shop_id . $this->pack_id . $this->adapte_gaucher_id;
    return $this;
}

Both methods yield a null value since all three variables are currently null. Is there a way to override this?

Appreciate any advice. Thank you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In order to obtain three different arrays as output, what is the step-by-step process for writing this query using PDO?

I need assistance with converting this SQL query into PDO format: SELECT COUNT( testRunID ) , platform, SUM( IF( STATUS = 'passed', 1, 0 ) ) passed_count, SUM( IF( STATUS = 'failed', 1, 0 ) ) failed_count, SUM( IF( STATUS = 'inc ...

PHP htaccess error: Rewrite rules causing 500 ERROR for nonexistent pages

I created a website that cleverly hides the .php extension using RewriteRule and allows the script to determine parameters based on slashes. For example: The main script is deals.php which processes parameters such as new york, Groupon, and the deal ID. ...

Passing data from PHP to jQuery in a file upload script

I'm facing a problem that I can't seem to solve on my own. What I am trying to achieve is to upload a file to the server via PHP (upload.php), generate a new filename, and then pass that new filename to my webpage (upload.html). Within my uploa ...

Executing a Codeigniter PHP function within a controller using Cron Jobs on cPanel every hour

Is it possible to run a PHP function in the controller every hour using cPanel Cron Jobs? During testing, I have decided to execute this function every minute. An example implementation is shown below: class Mycontroller extends CI_Controller { pu ...

Is PHP $_SESSION causing a hindrance in loading your website?

I have a basic webpage to check if sessions are properly set or not. session_start(); if (!isset($_SESSION['id'])) { $_SESSION['checkin'] = 'yes'; header("Location: https://blabla.php"); exit(); } else { // do ...

Why do we use array[] instead of just array when creating a new stdClass object?

$arr = []; $arr[] = new stdClass; //this adds an object to the array $arr = new stdClass; //this changes arr into an object It's peculiar because $arr was initially declared as an array. If you remove the brackets in $arr = new stdClass; then $arr ...

Add a fresh text field with the click of a button and delete it with another button in Laravel 4

My form includes two fields: phone and email, as shown in the image below. By clicking on the plus button, I would like to add an additional text field to the form below the button. Similarly, by clicking on the minus button, I want to remove the text fie ...

After establishing the connection between Ajax and PHP with a relationship table, the inserted data is not being displayed

I am new to using Ajax and Php and have a question about inserting data into a Mysql table using Bootstrap Modal, Ajax, and Php. Currently, I have a table named "tbl_employee" and a page called "index.php." In the "index.php" page, there is a Bootstrap m ...

AFNetworking failed to correctly deserialise a JSON-encoded dictionary within another dictionary

While working on my PHP backend, I have a situation where I am returning JSON data in the following format: $results_array=array(); $stmt->bind_result($IdUser,$username); while($stmt->fetch()) { $r ...

Regular expressions are compatible with JavaScript, but unfortunately, they are not supported

After successfully implementing this regex in my JavaScript for webpage validation, I attempted to incorporate it into my PHP script as a backup. However, every string I passed through the regex failed, even correct names. In an effort to resolve the issu ...

The Ajax Sqlite driver was unable to be located./SQLSTATE[HY000]

I am currently working on an exercise involving Ajax and a SQLite database using DB Browser for SQLite. The goal of the exercise is to populate two dropdown menus based on each other in cascade. However, I am facing difficulty in populating the first dropd ...

The password entered does not match the hashed and salted password in PHP during login

Currently working on a project in Unity where I am trying to implement account creation, login functionality, and saving user data using PHP and MySQL. The registration process is successfully completed, however, I am facing issues with the login process. ...

Using PM2 to Manage Your PHP Scripts in Cluster Mode

Currently, I have been effectively managing single instances of PHP daemons with PM2, and so far everything is running smoothly! When it comes to managing Node.js/IO.js apps with PM2, I can easily launch them in cluster mode without any issues. However, t ...

Utilize a nested query, only to find that the inner query returned null results

I am trying to retrieve data from a table in my database and then use that data in another select query. However, when I attempt this, my final query returns null. Can anyone help me figure out what I did wrong? $query = "SELECT * FROM `DBovervieuw` WHERE ...

Converting PHP code to Typescript

Currently, I am working on developing a function for firebase that will trigger a POST request to call a specific URL. While I have successfully implemented GET requests, tackling the POST method has proven to be more challenging. I have some sample code ...

Display a message after serializing JSON in my cakePHP application

Is there a way to append something at the conclusion of the cakePHP json/xml output? This is necessary for incorporating JSONP capability (Since I must insert the callback at the start and ');' at the end) This is how the controller is set up: ...

Using Laravel 5.1 to Redirect and Pass Get Parameters

I am currently troubleshooting a redirect issue in Laravel 5.1 that is leading to the following error NotFoundHttpException in RouteCollection.php line 161: The specific redirect I am attempting to accomplish is as follows: http://example.com/tool/view. ...

Has this OOD been implemented accurately?

Currently, I am diving into the world of object-oriented SOLID principles and design patterns. To practice, I decided to take a problem from my ongoing project and work on designing a solution for it. I would greatly appreciate your feedback on whether my ...

Creating a dynamic variable in Laravel 5.8 is quite simple. You can achieve this by using the "?" symbol as a

Here's an example of how to set parameters: For instance: http://localhost/firecek_web/pengaduan?barcode=8571385 Define the route: Route::get('/pengaduan','PengaduanController@index'); ...

Empty Json Index

I have developed an Ionic application that communicates with my Laravel-based API by sending JSON data. Whenever a post request is made to the designated route, I invoke a method on my controller. This controller then takes the received data and stores i ...