Neko Zhang
Neko Zhang

Reputation: 15

How to use repository method findByxx about field name with underline(_) in symfony

// This is my entity class object
use Doctrine\ORM\Mapping as ORM;
class PayOrder {
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(type="string",unique=true)
     */
    private $pay_no;
};
// I use it in my function, this is the Repository
use Doctrine\ORM\EntityManager;
use Exception;
use Psr\Container\ContainerInterface;
use Doctrine\ORM\Mapping;
class PayOrderRepository extends \Doctrine\ORM\EntityRepository {
    public function get( PayOrder $payOrder ) {
        $pay_no=$payOrder->getPayNo();
        // It will occurs a exception, how to fix it, any one can help me?
        return $this->findBypayno($pay_no);
    }
};

Besides that, I can't find the document to fix the problem. I want to use the field name pay_no, and I want to use the repository findbyxxx, but I do not how to use it correctly.

Upvotes: 1

Views: 1402

Answers (2)

hacfi
hacfi

Reputation: 766

Or just use

$this->findBy(['pay_no' => $payOrder->getPayNo());

The findByX method are just intercepted method calls - see line up 177 to 179 that are transformed to findBy calls.

Upvotes: 0

Mert Öksüz
Mert Öksüz

Reputation: 1071

Change your property name to $payNo.

Probably in your entity look like that;

//...

/**
 * @ORM\Column(type="string", length=255)
 */
$pay_no

//....

Change it like that;

//...

/**
 * @ORM\Column(name="pay_no", type="string", length=255)
 */
$payNo

//....

After that,

  1. Remove old getter/setter for $pay_no
  2. For Symfony2 run app/console doctrine:generate:entities
  3. For Symfony4 run bin/console make:entity --regenerate

Goodluck. If you have any question or blocker please write me.

Upvotes: 2

Related Questions