Using Entity Class

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
muttou
User
Posts: 135

Using Entity Class

Post by muttou »

Hi, I am using phpmaker 2024, need to use Entity classes in userfn.php files using api class, following method/code is working fine but showing empty result. Kindly help me out this that how we can use Entity classes properly in custom code/API's. my code is as below

$em = EntityManager( );
$customer = $em->getRepository(Entity\Customer::class)->find("1");   //Customer.php exists in src/Entity folder.

Make ensure that i am using this under Api_Action($app) method using API calls. API is working but showing empty results (empty array). any help or support will highly be appreciated.

Thank you.


arbei
User
Posts: 9787

Post by arbei »

Make sure:

  1. The customers table is in the main database (since you use EntityManager()), not a linked table,
  2. The primary key of the table is of string type (since you use "1") and the record with that value exists.
  3. Debug your code by, e.g. var_dump($customer). Turn on Debug.

muttou
User
Posts: 135

Post by muttou »

Thank you for your response, using debug option, its shows only empty array, both $em & $customer objects, also try all possible methods but no success. even use this method $em->getRepository(Entity\Customer::class) and debug, again empty array. I have been searching & going through the help in forum but no success.

Also use this method

$entityClass = GetEntityClass("customers");
$customer = GetUserEntityManager()->getRepository($entityClass);
var_dump($customer);

same empty arrays show in result.

I am using under custom API method. hope entity classes work fine under ajax GET/POST methods.

Thank you for your positive response & further directions.


arbei
User
Posts: 9787

Post by arbei »

You may your complete code in userfn.php for discussion. If you tried to use directly in Global Code (not in Api_Action), it won't work because the container is not built yet. (You should see errors if you have enabled Debug, don't skip.)


muttou
User
Posts: 135

Post by muttou »

Hi, This is working fine using findAll() and findBy() menthod, its giving records in array but not showing attribute values in table schema like customer name, id, cell and all table attributes. sample code is below that is working fine and giving results as expected. Problem is not giving attribute values. Thank you once again for your help.

$em = EntityManager();
$customer = $em->getRepository(Entity\Customer::class)->findBy(['cell' => $cell]);
var_dump($customer);

muttou
User
Posts: 135

Post by muttou »

This code is working fine under custom files as mentioned below.


use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

require_once "vendor/autoload.php";

   $paths = ['/src/Entity'];
    $isDevMode = false;

    // the connection configuration
    $dbParams = [
        'driver'   => 'pdo_mysql',
        'user'     => 'root',
        'password' => '123456',
        'dbname'   => 'crm',
    ];

    $config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
    $connection = DriverManager::getConnection($dbParams, $config);
    $em = new EntityManager($connection, $config);
    
    $customer = $em->getRepository(Entity\Customer::class)->findAll();
    //->findOneBy(["customerId" => 1]);
    var_dump($customer);

arbei
User
Posts: 9787

Post by arbei »

If you meant find() does not work, then you may not have set the "customerId" as primary key of your table or you have composite key (which will not work with find()) or the data type of the primary key value you search does not match that of the table, as said above.


muttou
User
Posts: 135

Post by muttou »

No its not such a case. find() method is working fine and other methods too. Its shows array length (records) but don't show the meta data & values (table columns not appearing). As per the code below it is fetching the matched customer based upon cell number and in response show array length 1. but columns are not appearing in $customer object.

any help or support will highly be appreciated. Thank you once again.

$em = EntityManager();
$customer = $em->getRepository(Entity\Customer::class)->findBy(['cell' => $cell]);
var_dump($customer);

sticcino
User
Posts: 1090

Post by sticcino »

hello,

I just tested your code with one of our tables, and it appears it worked - find() returned 4445 rows with all columns and data returned in the array..


sticcino
User
Posts: 1090

Post by sticcino »

also tested your code:

$em = EntityManager();
$customer = $em->getRepository(Entity\Customer::class)->findBy(['cell' => $cell]);
var_dump($customer);

as:

    $record = $em->getRepository(Entity\Washroom::class)->findBy(['id' => 1453]);   

it returned the record with key value 1453 with all the corresponding data and fields as expected


muttou
User
Posts: 135

Post by muttou »

but at my end. meta info is not appearing. (columns data). kindly make it run under your code. an API call to fetch the customer based upon cell number. Try at your end & check the result.

Thank you once again.

function Api_Action($app)
{
    $app->get('/fetch_customer/{cell}', function ($request, $response, $args) {
        
        $cell = $args["cell"] ?? null; // Get the input value
        
        //$dbids = array_keys(Config("Databases"));
        //$paths = ["src/Entity"];
        //$isDevMode = true;

        $em =  EntityManager();    
         
        if ($cell !== null) {

            $entityClass = GetEntityClass("customers");
	    $customer = $em->getRepository(Entity\Customer::class)->findBy(['cell' => $cell]);
            //$customer = GetUserEntityManager()->getRepository($entityClass)->findAll();
            $response = $response->withJson([ "Cell" => $cell, "Customer" => $customer ]);
        }
        return $response; // Return Psr\Http\Message\ResponseInterface object
    });
}

arbei
User
Posts: 9787

Post by arbei »

Note that findBy() returns array of entities (not array or object). You need to convert entity to array by the entity's toArray() so you can do json_encode().


muttou
User
Posts: 135

Post by muttou »

Oky but in my code findBy also returning the same array against matched value but not showing the values. sample code share. kindly assist in this regards. moreover, how can i get the connection object in order to initiate the EntityManager($con, $config) in custom files using maker generated code/functions.

Thank you for your positive response.

$em = new EntityManager($connection, $config);

arbei
User
Posts: 9787

Post by arbei »

All entity repository methods returns entity or array of entities, not assciative array, not stdClass object. You can see the details by, for example, var_dump($customer). As said above, you need to write a little more code to convert entity to array first.

If you just to output records as JSON, there is no need to use entities, simply use ExecuteJson() or ExecuteRows().


muttou
User
Posts: 135

Post by muttou »

Finally get the results. Thank you for overall support and brain storming. Bravo.

$customer = GetUserEntityManager()->getRepository( GetEntityClass("customers"))->findOneBy(["cell" => $cell]);

echo $customer->getCustomerName();
echo $customer->getCell();
echo $customer->getCustomerId();

shows required results.


muttou
User
Posts: 135

Post by muttou »

"Note that findBy() returns array of entities (not array or object). You need to convert entity to array by the entity's toArray() so you can do json_encode()."

Also As per your directions this one is working as expected.

Finally retrieve the array of object.

$customer = GetUserEntityManager()->getRepository( GetEntityClass("customers"))->findOneBy(["cell" => $cell])->toArray();

Many Thanks


Post Reply