0

how can i make a database query with extbase to check if a database entry already exist? I know how to do that with php but not with extbase syntax.

I want to add a user to the database. That works. But the user should only be added if the regId doesn't already exist.

This is my code so far:

/**
 * action registerDevice  
 * @param Tx_xxx_Domain_Repository_UserRepository $muserRepository 
 * @return void
 */

public function registerDeviceAction(){

    $userRepository = $this->objectManager->get('Tx_xxx_Domain_Repository_UserRepository');
    $user = $this->objectManager->create('Tx_xxx_Domain_Model_User');

    $allUser = $this->userRepository->findAll();

    if ( isset($_POST["regId"]) && $_GET['os'] ) {

        $regDevice = $_POST["regId"];
        $regMobileOs = $_GET["os"];


        $user->setMobileOs( $regMobileOs );
        $user->setFirstName('TEST');
        $user->setRegId( $regDevice );  



        $this->userRepository->add($user);

        $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
        $persistenceManager->persistAll();


        }

    }

Thank you very much! :)

Best regards

Pascal Cloverfield
  • 561
  • 1
  • 6
  • 20

1 Answers1

1

Since Extbase 1.1 you can use the Magic Method find(One)ByProperty and countByProperty.

So, without creating a new method you can use something like:

if(!$this->someRepository->countByRegId($regId)) {
    // there isnt a object with $regId yet
}
Arek van Schaijk
  • 1,432
  • 11
  • 34