本文整理汇总了PHP中Espo\ORM\Entity::isNew方法的典型用法代码示例。如果您正苦于以下问题:PHP Entity::isNew方法的具体用法?PHP Entity::isNew怎么用?PHP Entity::isNew使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Espo\ORM\Entity
的用法示例。
在下文中一共展示了Entity::isNew方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
public function process(Entity $entity)
{
if ($entity->has('assignedUserId') && $entity->get('assignedUserId')) {
$assignedUserId = $entity->get('assignedUserId');
if ($assignedUserId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) {
$notification = $this->getEntityManager()->getEntity('Notification');
$notification->set(array('type' => 'Assign', 'userId' => $assignedUserId, 'data' => array('entityType' => $entity->getEntityType(), 'entityId' => $entity->id, 'entityName' => $entity->get('name'), 'isNew' => $entity->isNew(), 'userId' => $this->getUser()->id, 'userName' => $this->getUser()->get('name'))));
$this->getEntityManager()->saveEntity($notification);
}
}
}
示例2: save
public function save(Entity $entity, array $options = array())
{
$isNew = $entity->isNew();
$result = parent::save($entity, $options);
if ($isNew) {
if (!empty($entity->id) && $entity->has('contents')) {
$contents = $entity->get('contents');
$this->getFileManager()->putContents('data/upload/' . $entity->id, $contents);
}
}
return $result;
}
示例3: afterSave
public function afterSave(Entity $entity)
{
if ($entity->isNew()) {
$parentType = $entity->get('parentType');
$parentId = $entity->get('parentId');
$superParentType = $entity->get('superParentType');
$superParentTypeId = $entity->get('superParentTypeId');
$userIdList = [];
if ($parentType && $parentId) {
$userIdList = array_merge($userIdList, $this->getSubscriberIdList($parentType, $parentId));
}
if ($superParentType && $superParentTypeId) {
$userIdList = array_merge($userIdList, $this->getSubscriberIdList($superParentType, $superParentTypeId));
}
//$userIdList = array_merge($userIdList, $this->getMentionedUserIdList($entity));
$userIdList = array_unique($userIdList);
if (!empty($userIdList)) {
$this->getNotificationService()->notifyAboutNote($userIdList, $entity);
}
}
}
示例4: beforeSave
protected function beforeSave(Entity $entity)
{
if ($entity->isNew()) {
$userName = $entity->get('userName');
if (empty($userName)) {
throw new Error();
}
$user = $this->where(array('userName' => $userName))->findOne();
if ($user) {
throw new Error();
}
} else {
if ($entity->isFieldChanged('userName')) {
$userName = $entity->get('userName');
if (empty($userName)) {
throw new Error();
}
$user = $this->where(array('userName' => $userName, 'id!=' => $entity->id))->findOne();
if ($user) {
throw new Error();
}
}
}
}
示例5: isPermittedTeams
public function isPermittedTeams(Entity $entity)
{
$assignmentPermission = $this->getAcl()->get('assignmentPermission');
if (empty($assignmentPermission) || $assignmentPermission === true || !in_array($assignmentPermission, ['team', 'no'])) {
return true;
}
if (!$entity->hasField('teamsIds')) {
return true;
}
$teamIds = $entity->get('teamsIds');
if (empty($teamIds)) {
return true;
}
$newIds = [];
if (!$entity->isNew()) {
$existingIds = [];
foreach ($entity->get('teams') as $team) {
$existingIds[] = $team->id;
}
foreach ($teamIds as $id) {
if (!in_array($id, $existingIds)) {
$newIds[] = $id;
}
}
} else {
$newIds = $teamIds;
}
if (empty($newIds)) {
return true;
}
$userTeamIds = $this->getUser()->get('teamsIds');
foreach ($newIds as $id) {
if (!in_array($id, $userTeamIds)) {
return false;
}
}
return true;
}
示例6: save
public function save(Entity $entity, array $options = array())
{
$nowString = date('Y-m-d H:i:s', time());
$restoreData = array();
if ($entity->isNew()) {
if (!$entity->has('id')) {
$entity->set('id', Util::generateId());
}
if ($entity->hasField('createdAt')) {
$entity->set('createdAt', $nowString);
}
if ($entity->hasField('modifiedAt')) {
$entity->set('modifiedAt', $nowString);
}
if ($entity->hasField('createdById')) {
$entity->set('createdById', $this->entityManager->getUser()->id);
}
if ($entity->has('modifiedById')) {
$restoreData['modifiedById'] = $entity->get('modifiedById');
}
if ($entity->has('modifiedAt')) {
$restoreData['modifiedAt'] = $entity->get('modifiedAt');
}
$entity->clear('modifiedById');
} else {
if (empty($options['silent'])) {
if ($entity->hasField('modifiedAt')) {
$entity->set('modifiedAt', $nowString);
}
if ($entity->hasField('modifiedById')) {
$entity->set('modifiedById', $this->entityManager->getUser()->id);
}
}
if ($entity->has('createdById')) {
$restoreData['createdById'] = $entity->get('createdById');
}
if ($entity->has('createdAt')) {
$restoreData['createdAt'] = $entity->get('createdAt');
}
$entity->clear('createdById');
$entity->clear('createdAt');
}
$this->restoreData = $restoreData;
$result = parent::save($entity, $options);
return $result;
}
示例7: storeEntityEmailAddress
public function storeEntityEmailAddress(Entity $entity)
{
$email = trim($entity->get('emailAddress'));
$emailAddressData = null;
if ($entity->has('emailAddressData')) {
$emailAddressData = $entity->get('emailAddressData');
}
$pdo = $this->getEntityManager()->getPDO();
if ($emailAddressData !== null && is_array($emailAddressData)) {
$previousEmailAddressData = array();
if (!$entity->isNew()) {
$previousEmailAddressData = $this->getEmailAddressData($entity);
}
$hash = array();
foreach ($emailAddressData as $row) {
$key = $row->emailAddress;
if (!empty($key)) {
$hash[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false);
}
}
$hashPrev = array();
foreach ($previousEmailAddressData as $row) {
$key = $row->emailAddress;
if (!empty($key)) {
$hashPrev[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false);
}
}
$primary = false;
$toCreate = array();
$toUpdate = array();
$toRemove = array();
foreach ($hash as $key => $data) {
$new = true;
$changed = false;
if ($hash[$key]['primary']) {
$primary = $key;
}
if (array_key_exists($key, $hashPrev)) {
$new = false;
$changed = $hash[$key]['optOut'] != $hashPrev[$key]['optOut'] || $hash[$key]['invalid'] != $hashPrev[$key]['invalid'];
if ($hash[$key]['primary']) {
if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) {
$primary = false;
}
}
}
if ($new) {
$toCreate[] = $key;
}
if ($changed) {
$toUpdate[] = $key;
}
}
foreach ($hashPrev as $key => $data) {
if (!array_key_exists($key, $hash)) {
$toRemove[] = $key;
}
}
foreach ($toRemove as $address) {
$emailAddress = $this->getByAddress($address);
if ($emailAddress) {
$query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `deleted` = 1, `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . "\n\t\t\t\t\t\t";
$sth = $pdo->prepare($query);
$sth->execute();
}
}
foreach ($toUpdate as $address) {
$emailAddress = $this->getByAddress($address);
if ($emailAddress) {
$emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid']));
$this->save($emailAddress);
}
}
foreach ($toCreate as $address) {
$emailAddress = $this->getByAddress($address);
if (!$emailAddress) {
$emailAddress = $this->get();
$emailAddress->set(array('name' => $address, 'optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid']));
$this->save($emailAddress);
} else {
if ($emailAddress->get('optOut') != $hash[$address]['optOut'] || $emailAddress->get('invalid') != $hash[$address]['invalid']) {
$emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid']));
$this->save($emailAddress);
}
}
$query = "\n\t\t\t\t\t\tINSERT entity_email_address \n\t\t\t\t\t\t\t(entity_id, entity_type, email_address_id, `primary`)\n\t\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->getEntityName()) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($emailAddress->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($address === $primary) . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t";
$sth = $pdo->prepare($query);
$sth->execute();
}
if ($primary) {
$emailAddress = $this->getByAddress($primary);
if ($emailAddress) {
$query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\t`primary` = 1 AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t";
$sth = $pdo->prepare($query);
$sth->execute();
$query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 1\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . " AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t";
$sth = $pdo->prepare($query);
$sth->execute();
}
}
//.........这里部分代码省略.........
示例8: findRelated
public function findRelated(Entity $entity, $relationName, array $params = array())
{
if ($entity->isNew()) {
return;
}
$entityName = $entity->relations[$relationName]['entity'];
$this->getEntityManager()->getRepository($entityName)->handleSelectParams($params);
$result = $this->getMapper()->selectRelated($entity, $relationName, $params);
if (is_array($result)) {
$collection = new EntityCollection($result, $entityName, $this->entityFactory);
return $collection;
} else {
return $result;
}
}
示例9: save
public function save(Entity $entity)
{
$nowString = date('Y-m-d H:i:s', time());
$restoreData = array();
if ($entity->isNew()) {
if (!$entity->has('id')) {
$entity->set('id', uniqid());
}
if ($entity->hasField('createdAt')) {
$entity->set('createdAt', $nowString);
}
if ($entity->hasField('createdById')) {
$entity->set('createdById', $this->entityManager->getUser()->id);
}
if ($entity->has('modifiedById')) {
$restoreData['modifiedById'] = $entity->get('modifiedById');
}
if ($entity->has('modifiedAt')) {
$restoreData['modifiedAt'] = $entity->get('modifiedAt');
}
$entity->clear('modifiedById');
$entity->clear('modifiedAt');
} else {
if ($entity->hasField('modifiedAt')) {
$entity->set('modifiedAt', $nowString);
}
if ($entity->hasField('modifiedById')) {
$entity->set('modifiedById', $this->entityManager->getUser()->id);
}
if ($entity->has('createdById')) {
$restoreData['createdById'] = $entity->get('createdById');
}
if ($entity->has('createdAt')) {
$restoreData['createdAt'] = $entity->get('createdAt');
}
$entity->clear('createdById');
$entity->clear('createdAt');
}
$result = parent::save($entity);
$entity->set($restoreData);
$this->handleEmailAddressSave($entity);
$this->handlePhoneNumberSave($entity);
$this->handleSpecifiedRelations($entity);
return $result;
}
示例10: checkAssignment
public function checkAssignment(Entity $entity)
{
if ($entity->isNew()) {
$targetType = $entity->get('targetType');
if ($targetType) {
$assignmentPermission = $this->getAcl()->get('assignmentPermission');
if ($assignmentPermission === false || $assignmentPermission === 'no') {
if ($targetType !== 'self') {
throw new Forbidden('Not permitted to post to anybody except self.');
}
}
if ($targetType === 'teams') {
$teamIdList = $entity->get('teamsIds');
if (empty($teamIdList) || !is_array($teamIdList)) {
throw new BadRequest();
}
}
if ($targetType === 'users') {
$userIdList = $entity->get('usersIds');
if (empty($userIdList) || !is_array($userIdList)) {
throw new BadRequest();
}
}
if ($assignmentPermission === 'team') {
if ($targetType === 'all') {
throw new Forbidden('Not permitted to post to all.');
}
$userTeamIdList = $this->getUser()->getTeamIdList();
if ($targetType === 'teams') {
if (empty($userTeamIdList)) {
throw new Forbidden('Not permitted to post to foreign teams.');
}
foreach ($teamIdList as $teamId) {
if (!in_array($teamId, $userTeamIdList)) {
throw new Forbidden('Not permitted to post to foreign teams.');
}
}
} else {
if ($targetType === 'users') {
if (empty($userTeamIdList)) {
throw new Forbidden('Not permitted to post to users from foreign teams.');
}
foreach ($userIdList as $userId) {
if ($userId === $this->getUser()->id) {
continue;
}
if (!$this->getEntityManager()->getRepository('User')->checkBelongsToAnyOfTeams($userId, $userTeamIdList)) {
throw new Forbidden('Not permitted to post to users from foreign teams.');
}
}
}
}
}
}
}
return true;
}
示例11: storeEntityPhoneNumber
public function storeEntityPhoneNumber(Entity $entity)
{
$phone = trim($entity->get('phoneNumber'));
$phoneNumberData = null;
if ($entity->has('phoneNumberData')) {
$phoneNumberData = $entity->get('phoneNumberData');
}
$pdo = $this->getEntityManager()->getPDO();
if ($phoneNumberData !== null && is_array($phoneNumberData)) {
$previousPhoneNumberData = array();
if (!$entity->isNew()) {
$previousPhoneNumberData = $this->getPhoneNumberData($entity);
}
$hash = array();
foreach ($phoneNumberData as $row) {
$key = $row->phoneNumber;
if (!empty($key)) {
$hash[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type);
}
}
$hashPrev = array();
foreach ($previousPhoneNumberData as $row) {
$key = $row->phoneNumber;
if (!empty($key)) {
$hashPrev[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type);
}
}
$primary = false;
$toCreate = array();
$toUpdate = array();
$toRemove = array();
foreach ($hash as $key => $data) {
$new = true;
$changed = false;
if ($hash[$key]['primary']) {
$primary = $key;
}
if (array_key_exists($key, $hashPrev)) {
$new = false;
$changed = $hash[$key]['type'] != $hashPrev[$key]['type'];
if ($hash[$key]['primary']) {
if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) {
$primary = false;
}
}
}
if ($new) {
$toCreate[] = $key;
}
if ($changed) {
$toUpdate[] = $key;
}
}
foreach ($hashPrev as $key => $data) {
if (!array_key_exists($key, $hash)) {
$toRemove[] = $key;
}
}
foreach ($toRemove as $number) {
$phoneNumber = $this->getByNumber($number);
if ($phoneNumber) {
$query = "\n DELETE FROM entity_phone_number\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . "\n ";
$sth = $pdo->prepare($query);
$sth->execute();
}
}
foreach ($toUpdate as $number) {
$phoneNumber = $this->getByNumber($number);
if ($phoneNumber) {
$phoneNumber->set(array('type' => $hash[$number]['type']));
$this->save($phoneNumber);
}
}
foreach ($toCreate as $number) {
$phoneNumber = $this->getByNumber($number);
if (!$phoneNumber) {
$phoneNumber = $this->get();
$phoneNumber->set(array('name' => $number, 'type' => $hash[$number]['type']));
$this->save($phoneNumber);
} else {
if ($phoneNumber->get('type') != $hash[$number]['type']) {
$phoneNumber->set(array('type' => $hash[$number]['type']));
$this->save($phoneNumber);
}
}
$query = "\n INSERT entity_phone_number\n (entity_id, entity_type, phone_number_id, `primary`)\n VALUES\n (\n " . $pdo->quote($entity->id) . ",\n " . $pdo->quote($entity->getEntityName()) . ",\n " . $pdo->quote($phoneNumber->id) . ",\n " . $pdo->quote((int) ($number === $primary)) . "\n )\n ";
$sth = $pdo->prepare($query);
$sth->execute();
}
if ($primary) {
$phoneNumber = $this->getByNumber($primary);
if ($phoneNumber) {
$query = "\n UPDATE entity_phone_number\n SET `primary` = 0\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n `primary` = 1 AND\n deleted = 0\n ";
$sth = $pdo->prepare($query);
$sth->execute();
$query = "\n UPDATE entity_phone_number\n SET `primary` = 1\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . " AND \n deleted = 0\n ";
$sth = $pdo->prepare($query);
$sth->execute();
}
}
//.........这里部分代码省略.........
示例12: afterSave
public function afterSave(Entity $entity)
{
if ($entity->isNew()) {
$parentType = $entity->get('parentType');
$parentId = $entity->get('parentId');
$superParentType = $entity->get('superParentType');
$superParentTypeId = $entity->get('superParentTypeId');
$userIdList = [];
if ($parentType && $parentId) {
$userIdList = array_merge($userIdList, $this->getSubscriberIdList($parentType, $parentId));
if ($superParentType && $superParentTypeId) {
$userIdList = array_merge($userIdList, $this->getSubscriberIdList($superParentType, $superParentTypeId));
}
} else {
$targetType = $entity->get('targetType');
if ($targetType === 'users') {
$targetUserIdList = $entity->get('usersIds');
foreach ($targetUserIdList as $userId) {
if ($userId === $this->getUser()->id) {
continue;
}
$userIdList[] = $userId;
}
} else {
if ($targetType === 'teams') {
$targetTeamIdList = $entity->get('teamsIds');
foreach ($targetTeamIdList as $teamId) {
$team = $this->getEntityManager()->getEntity('Team', $teamId);
if (!$team) {
continue;
}
$targetUserList = $this->getEntityManager()->getRepository('Team')->findRelated($team, 'users', array('whereClause' => array('isActive' => true)));
foreach ($targetUserList as $user) {
if ($user->id === $this->getUser()->id) {
continue;
}
$userIdList[] = $user->id;
}
}
} else {
if ($targetType === 'all') {
$targetUserList = $this->getEntityManager()->getRepository('User')->find(array('whereClause' => array('isActive' => true)));
foreach ($targetUserList as $user) {
if ($user->id === $this->getUser()->id) {
continue;
}
$userIdList[] = $user->id;
}
}
}
}
}
$userIdList = array_unique($userIdList);
foreach ($userIdList as $i => $userId) {
if ($entity->isUserIdNotified($userId)) {
unset($userIdList[$i]);
}
}
$userIdList = array_values($userIdList);
if (!empty($userIdList)) {
$this->getNotificationService()->notifyAboutNote($userIdList, $entity);
}
}
}