php schema - Doctrine 2 mise à jour de l'entité




update dump (5)

Vous pouvez également utiliser getReference pour mettre à jour une propriété d'entité par identificateur sans récupérer l'état de la base de données.

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/advanced-configuration.html#reference-proxies

Cela établira un proxy simple pour travailler entité par ID au lieu d'instancier une new Entity ou d'obtenir explicitement l'entité de la base de données à l'aide de find() , qui peut ensuite être mise à jour par flush.

$data = $entityManager->getReference('ATest', $id);
$data->setName('ORM Tested');
$entityManager->flush();

Ceci est particulièrement utile pour mettre à jour les associations OneToMany ou ManyToMany d'une entité. EG: $case->addTest($data);

Il est généralement déconseillé de définir manuellement l'identificateur d'une nouvelle entité, même si l'intention est de mettre à jour l'entité. Au lieu de cela, il est généralement préférable de laisser EntityManager ou le constructeur Entity établir les identifiants appropriés, tels qu'un UUID . Pour cette raison, Doctrine générera des entités par défaut avec l'identifiant en tant que propriété privée sans méthode de définition.

Est-il possible de mettre à jour une entité de la même manière que ci-dessous:

$data       = new ATest();  // my entity
$data->id   = 1;            // id 1 already exists, I just want to update this row
$data->name = "ORM Tested"; // changed the name

$entityManager->persist($data);
$entityManager->flush();

Cela insérera et modifiera l'id de l'objet au lieu de mettre à jour la ligne existante dans la base de données.


Je devais utiliser

$entityManager->merge($data)

Vous devriez appeler merge au lieu de persist:

$data = new MyEntity();
$data->setId(123);
$data->setName('test');

$entityManager->merge($data);
$entityManager->flush();

Ou obtenez simplement l'entité gérée plutôt qu'une entité vide.

$data = $entityManager->getRepository('ATest')->findOne(1); // ATest is my entitity class
$data->name = "ORM Tested"; // just change the name

$entityManager->persist($data);
$entityManager->flush();

Si l'entité est déjà gérée, persist () la mettra à jour plutôt que d'insérer une nouvelle.


Warning: mysql_connect(): Access denied for user 'name'@'host'

This warning shows up when you connect to a MySQL/MariaDB server with invalid or missing credentials (username/password). So this is typically not a code problem, but a server configuration issue.

  • See the manual page on mysql_connect("localhost", "user", "pw") for examples.

  • Check that you actually used a $username and $password .

    • It's uncommon that you gain access using no password - which is what happened when the Warning: said (using password: NO) .
    • Only the local test server usually allows to connect with username root , no password, and the test database name.

    • You can test if they're really correct using the command line client:
      mysql --user="username" --password="password" testdb

    • Username and password are case-sensitive and whitespace is not ignored. If your password contains meta characters like $ , escape them, or put the password in single quotes .

    • Most shared hosting providers predeclare mysql accounts in relation to the unix user account (sometimes just prefixes or extra numeric suffixes). See the docs for a pattern or documentation, and CPanel or whatever interface for setting a password.

    • See the MySQL manual on Adding user accounts using the command line. When connected as admin user you can issue a query like:
      CREATE USER 'username'@'localhost' IDENTIFIED BY 'newpassword';

    • Or use Adminer or WorkBench or any other graphical tool to create, check or correct account details.

    • If you can't fix your credentials, then asking the internet to "please help" will have no effect. Only you and your hosting provider have permissions and sufficient access to diagnose and fix things.

  • Verify that you could reach the database server, using the host name given by your provider:
    ping dbserver.hoster.example.net

    • Check this from a SSH console directly on your webserver. Testing from your local development client to your shared hosting server is rarely meaningful.

    • Often you just want the server name to be "localhost" , which normally utilizes a local named socket when available. Othertimes you can try "127.0.0.1" as fallback.

    • Should your MySQL/MariaDB server listen on a different port, then use "servername:3306" .

    • If that fails, then there's a perhaps a firewall issue. (Off-topic, not a programming question. No remote guess-helping possible.)

  • When using constants like eg DB_USER or DB_PASSWORD , check that they're actually defined .

    • If you get a "Warning: Access defined for 'DB_USER'@'host'" and a "Notice: use of undefined constant 'DB_PASS'" , then that's your problem.

    • Verify that your eg xy/db-config.php was actually included and whatelse.

  • Check for correctly set GRANT permissions .

    • It's not sufficient to have a username + password pair.

    • Each MySQL/MariaDB account can have an attached set of permissions.

    • Those can restrict which databases you are allowed to connect to, from which client/server the connection may originate from, and which queries are permitted.

    • The "Access denied" warning thus may as well show up for mysql_query calls, if you don't have permissions to SELECT from a specific table, or INSERT / UPDATE , and more commonly DELETE anything.

    • You can adapt account permissions when connected per command line client using the admin account with a query like:
      GRANT ALL ON yourdb.* TO 'username'@'localhost';

  • If the warning shows up first with Warning: mysql_query(): Access denied for user ''@'localhost' then you may have a php.ini-preconfigured account/password pair .

    • Check that mysql.default_user= and mysql.default_password= have meaningful values.

    • Oftentimes this is a provider-configuration. So contact their support for mismatches.

  • Find the documentation of your shared hosting provider:

  • Note that you may also have depleted the available connection pool . You'll get access denied warnings for too many concurrent connections. (You have to investigate the setup. That's an off-topic server configuration issue, not a programming question.)

  • Your libmysql client version may not be compatible with the database server. Normally MySQL and MariaDB servers can be reached with PHPs compiled in driver. If you have a custom setup, or an outdated PHP version, and a much newer database server, or significantly outdated one - then the version mismatch may prevent connections. (No, you have to investigate yourself. Nobody can guess your setup).

More references:

Btw, you probably don't want to use mysql_* functions anymore . Newcomers often migrate to mysqli , which however is just as tedious. Instead read up on PDO and prepared statements .
$db = new PDO("mysql:host=localhost;dbname=testdb", "username", "password");







php orm doctrine-orm