Is it possible to make two columns work together?
I have tables which has
id |startdate |length |enddate
1 |2013-10-10 03:00:00 |60 |2013-10-10 04:00:00
2 |2013-10-11 04:00:00 |40 |2013-10-10 04:40:00
When I make new row.
I think enddate is possible to be calculated automatically
by values startdate and length.
What I want to do is:
1.input two value (length,startdate)
2.enddate is automatically calculated and keep updated.
How can I manage this?
reference:
my doctrine code is like this below
/**
* #var \DateTime
*
* #ORM\Column(name="startdate", type="datetime")
*/
private $startdate;
/**
* #var \DateTime
*
* #ORM\Column(name="enddate", type="datetime")
*/
private $enddate;
/**
*
* #ORM\Column(name="length",type="integer")
*/
private $length;
You can use events:
/** #PrePersist */
public function updateEndDate()
{
// this won't work because you need to use DateTime() but just as an example
$this->endDate = $this->startDate + $this->length ;
}
Check: http://docs.doctrine-project.org/en/2.0.x/reference/events.html#prepersist
and don't forget to put annotation:
/** #HasLifecycleCallbacks */
Related
I have two integers like val a = 5 and val b = 7. I just got curious about what is the fastest way to compute the minimum of these integers.
val min = minOf(a,b)
val min = Math.min(a,b)
val min = if(a<b) a else b
Can you tell me which one is faster and why that is faster?
They are all equally fast.
If you look at the definition of the minOf() function, you see
/**
* Returns the smaller of two values.
*/
#SinceKotlin("1.1")
#kotlin.internal.InlineOnly
public actual inline fun minOf(a: Int, b: Int): Int {
return Math.min(a, b)
}
that is, in fact, your second option.
Then, the body of Math.min() is
/**
* Returns the smaller of two {#code int} values. That is,
* the result the argument closer to the value of
* {#link Integer#MIN_VALUE}. If the arguments have the same
* value, the result is that same value.
*
* #param a an argument.
* #param b another argument.
* #return the smaller of {#code a} and {#code b}.
*/
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
that is, in fact, your third option.
KOTLIN 1.8.0
The latest one is below and working fine.
Latest : a.coerceAtMost(b)
Deprecated but still can use : Math.min(a, b)
Legacy : if(a>b) b else a
have a good day !
I am new to writing functions in C to be used with SQL. So far, I have looked at this example which executes a query using SPI. However, it prints the result as a log message. I was wondering how would this example have to change for me to return the result as a normal query (normal as in how I can view it when I execute a SQL query in pgAdmin)?
User-defined functions written in C may be what you want. https://www.postgresql.org/docs/current/xfunc-c.html#id-1.8.3.13.11
A complete example of returning a composite type looks like:
PG_FUNCTION_INFO_V1(retcomposite);
Datum
retcomposite(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
int call_cntr;
int max_calls;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/* switch to memory context appropriate for multiple function calls */
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* total number of tuples to be returned */
funcctx->max_calls = PG_GETARG_UINT32(0);
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
/*
* generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
MemoryContextSwitchTo(oldcontext);
}
/* stuff done on every call of the function */
funcctx = SRF_PERCALL_SETUP();
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
attinmeta = funcctx->attinmeta;
if (call_cntr < max_calls) /* do when there is more left to send */
{
char **values;
HeapTuple tuple;
Datum result;
/*
* Prepare a values array for building the returned tuple.
* This should be an array of C strings which will
* be processed later by the type input functions.
*/
values = (char **) palloc(3 * sizeof(char *));
values[0] = (char *) palloc(16 * sizeof(char));
values[1] = (char *) palloc(16 * sizeof(char));
values[2] = (char *) palloc(16 * sizeof(char));
snprintf(values[0], 16, "%d", 1 * PG_GETARG_INT32(1));
snprintf(values[1], 16, "%d", 2 * PG_GETARG_INT32(1));
snprintf(values[2], 16, "%d", 3 * PG_GETARG_INT32(1));
/* build a tuple */
tuple = BuildTupleFromCStrings(attinmeta, values);
/* make the tuple into a datum */
result = HeapTupleGetDatum(tuple);
/* clean up (this is not really necessary) */
pfree(values[0]);
pfree(values[1]);
pfree(values[2]);
pfree(values);
SRF_RETURN_NEXT(funcctx, result);
}
else /* do when there is no more left */
{
SRF_RETURN_DONE(funcctx);
}
}
You can combine this with SPI.
I have a function as follows:
/**
* #param \string[] ...$whitelist
* #return Array
*/
public function whitelist(string ...$whitelist): Array
{
// code
}
How can I pass an array (e.g $arr = [$val1, $val2, ...]) to the function?
Note that I cannot change the function structure because it belongs to a third party class I am using in my code.
/**
* #param \string[] ...$whitelist
* #return Array
*/
public function whitelist(string ...$whitelist): Array
{
// code
}
You pass the arguments by separating them by comma, meaning the whitelist function takes n number of arguments. You are using the splat operator (https://lornajane.net/posts/2014/php-5-6-and-the-splat-operator). So, in your case it may be something like:
whitelist(...['Item 1', 'Item 2', 'Item 3']);
We are migrating site from Intershop 6.4 to Intershop 7.9.
As suggested by Intershop support, we are trying to recreate all the auto-generated classes from the EDL model.
Here is the interface EDL code:
import "enfinity:/core/edl/com/intershop/beehive/core/types.edl";
import "enfinity:/core/edl/com/intershop/beehive/core/capi/domain/PersistentObject.edl";
import "enfinity:/core/edl/com/intershop/beehive/core/capi/domain/PersistentObjectPO.edl";
external PromotionCollection type "java.util.Collection<A1Promotion>";
namespace hr.a1.core.capi.promotion
{
cartridge interface A1Promotion extends PersistentObject
{
/**
* Promotion TMCode.
*/
attribute tmCode: string required;
/**
* Siebel promotion id.
*/
attribute siebelID: string required;
/**
* Promotion name.
*/
attribute name: string required;
/**
* Promotion description / currently URL to CW.
*/
attribute description: string;
/**
* Defines promotion priority / lower values first.
*/
attribute priority: int;
/**
* Holds link to tariff.
*/
attribute tariffID: uuid;
/**
* Contract type (A, M, R).
*/
attribute contractType: string required;
/**
* Contract binding (0, 12, 24).
*/
attribute contractBinding: string required;
/**
* Discount type / MF - monthly fee, HW - hardware, FU - free unit (not used at the moment)
*/
attribute discountType: string required;
/**
* Discount mode / P - percentage or F - fixed amount
*/
attribute discountMode: string;
/**
* Discount amount / either money value or percentage value / stored internally as Money
*/
attribute discountAmount: Money;
/*
* Flag which if it set disables checkbox of current promotion so user can't deselect it.
*/
attribute preselected: boolean;
/**
* Promotion validity / from date.
*/
attribute activeFrom: datetime;
/**
* Promotion validity / to date.
*/
attribute activeTo: datetime;
/**
* Promotion group / D - default, N - personalized, S% - Special
*/
attribute promotionGroup: string required;
/**
* Promotion type / S - single, G - group
*/
attribute promotionType: string required;
/**
* Free units amount type (MB, GB, SMS..)
*/
attribute units: string;
/**
* Group rule defined for promotion if promotion is group promotion
*/
attribute groupRule: string;
/**
* Name of the coupon list for the coupon promotions
*/
attribute couponListName: string;
/**
* HW promotion discount id
*/
attribute discountId: int;
}
}
and this is the PO EDL code:
import "enfinity:/app_core_a1/edl/app_core_a1/capi/A1Promotion.edl";
import "enfinity:/xcs/edl/com/intershop/beehive/xcs/internal/product/ProductPO.edl";
import "enfinity:/core/edl/com/intershop/beehive/core/types.edl";
import "enfinity:/core/edl/com/intershop/beehive/core/capi/domain/PersistentObjectPO.edl";
namespace hr.a1.core.internal.promotion
{
/*
* #author dragisic
*/
orm class A1PromotionPO extends PersistentObjectPO implements A1Promotion table "A1Promotion"
{
/**
* Promotion id.
*/
attribute tmCode : string<25> required column "tm_code";
/**
* Siebel promotion id.
*/
attribute siebelID : string<25> required column "siebel_id";
/**
* Promotion name.
*/
attribute name : string<100> required column "name";
/**
* Promotion description / currently URL to CW.
*/
attribute description : string<255> column "description";
/**
* Defines promotion priority / lower values first.
*/
attribute priority : int column "priority";
/**
* Holds link to tariff.
*/
attribute tariffID : uuid;
/**
* Contract type (A, M, R).
*/
attribute contractType : string<1> required column "contract_type";
/**
* Contract binding (0, 12, 24).
*/
attribute contractBinding : string<4> required column "contract_binding";
/**
* Discount type / MF - monthly fee, HW - hardware, FU - free unit (not used at the moment)
*/
attribute discountType : string<2> required column "discount_type";
/**
* Discount mode / P - percentage or F - fixed amount
*/
attribute discountMode : string<1> column "discount_mode";
/**
* Discount amount / either money value or percentage value / stored internally as Money
*/
attribute discountAmount : Money column "discount_amount";
/**
* Flag which if it set disables checkbox of current promotion so user can't deselect it.
*/
attribute preselected : boolean column "is_preselected";
/**
* Promotion validity / from date.
*/
attribute activeFrom : datetime column "active_from";
/**
* Promotion validity / to date.
*/
attribute activeTo : datetime column "active_to";
/**
* Promotion group / D - default, N - personalized, S% - Special
*/
attribute promotionGroup : string<3> required column "promo_group";
/**
* Promotion type / S - single, G - group
*/
attribute promotionType : string<1> required column "promo_type";
/**
* Free units amount type (MB, SMS..)
*/
attribute units : string<4> column "units";
/**
* Group rule defined for promotion if promotion is group promotion
*/
attribute groupRule : string<10> column "group_rule";
/**
* Name of the coupon list for the coupon promotions
*/
attribute couponListName: string<100> column "coupon_list_name";
/**
* Defines HW promotion discount id.
*/
attribute discountId : int column "discount_id";
/**
* Declare index in search items.
*/
index (contractType, contractBinding);
}
}
However, when we generate the code with the "Code generator for Intershop version 7.2 and later" we get the following compilation errors in the auto-generated class A1PromotionPOKey.java:
"The constructor Object(String) is undefined"
"The hierarchy of the type A1PromotionPOKey is inconsistent"
The auto-generated class looks like this:
// =============================================================================
// File: A1PromotionPOKey.java
// Generated by JGen Code Generator from INTERSHOP Communications AG.
// Generator template: ORMKey.xpt(checksum: b5b09bf4e9329db7d7dafe870b159b0d)
// =============================================================================
// The JGen Code Generator software is the property of INTERSHOP Communications AG.
// Any rights to use are granted under the license agreement.
// =============================================================================
package hr.a1.core.internal.promotion;
import com.intershop.beehive.core.capi.domain.PersistentObjectPOKey;
/**
* This class represents the primary key for objects of type A1PromotionPO.
* The key objects can be used for lookup operations in the database.
*
* #author dragisic
* #generated
*/
public class A1PromotionPOKey extends PersistentObjectPOKey {
/**
* Useless constant to make compiler happy.
*
* #generated
*/
private static final long serialVersionUID = 1L;
/**
* Creates an empty primary key. After creation of a new key object
* you must call the corresponding set<i>Attribute</i> method(s) to
* set the value(s) of the primary key.
*
* #generated
*/
public A1PromotionPOKey() {
}
/**
* Creates a primary key with the specified value(s).
*
* #generated
*/
public A1PromotionPOKey(
String UUID) {
super(UUID);
}
}
We have checked the gradle dependencies and the PersistentObjectPOKey import looks fine and the containing jar is added in the cartridge. We have also tried restarting Studio and refreshing the project, but it doesn't help.
Same EDL model works fine and produces compilable auto-generated code in Enfinity Suite 6.4.
EDIT:
Also, Im attaching errors from the console log during gradle build of the project.
:app_core_a1:compileJavaC:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOKey.java:20: error: cannot access CacheClearKey
public class A1PromotionPOKey extends
^
class file for com.intershop.beehive.cache.capi.common.CacheClearKey not found
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\capi\promotion\A1PromotionMgr.java:11: warning: [deprecation] Manager in com.intershop.beehive.core.capi.common has been deprecated
import com.intershop.beehive.core.capi.common.Manager;
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:14: warning: [deprecation] UUIDMgr in com.intershop.beehive.core.capi.util has been deprecated
import com.intershop.beehive.core.capi.util.UUIDMgr;
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\capi\promotion\A1PromotionMgr.java:11: warning: [deprecation] Manager in com.intershop.beehive.core.capi.common has been deprecated
import com.intershop.beehive.core.capi.common.Manager;
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:14: warning: [deprecation] UUIDMgr in com.intershop.beehive.core.capi.util has been deprecated
import com.intershop.beehive.core.capi.util.UUIDMgr;
^
warning: The following options were not recognized by any processor: '[componentName]'
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\capi\promotion\A1PromotionMgr.java:11: warning: [deprecation] Manager in com.intershop.beehive.core.capi.common has been deprecated
import com.intershop.beehive.core.capi.common.Manager;
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:14: warning: [deprecation] UUIDMgr in com.intershop.beehive.core.capi.util has been deprecated
import com.intershop.beehive.core.capi.util.UUIDMgr;
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\capi\promotion\A1PromotionMgr.java:24: warning: [deprecation] Manager in com.intershop.beehive.core.capi.common has been deprecated
Manager {
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPO.java:120: error: incompatible types: A1PromotionPOKey cannot be converted to PersistentObjectPOKey
super(factory, key);
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:82: error: incompatible types: A1PromotionPOKey cannot be converted to ORMObjectKey
A1PromotionPO instance = (A1PromotionPO) getEngine().getPersistenceManager().createObject(key, getClassDescription());
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:110: warning: [deprecation] UUIDMgr in com.intershop.beehive.core.capi.util has been deprecated
UUIDMgr.getInstance().createUUIDString(), domain.getUUID(), tmCode, siebelID, name, contractType, contractBinding, discountType, promotionGroup, promotionType);
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:110: warning: [deprecation] getInstance() in UUIDMgr has been deprecated
UUIDMgr.getInstance().createUUIDString(), domain.getUUID(), tmCode, siebelID, name, contractType, contractBinding, discountType, promotionGroup, promotionType);
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:110: warning: [deprecation] createUUIDString() in UUIDMgr has been deprecated
UUIDMgr.getInstance().createUUIDString(), domain.getUUID(), tmCode, siebelID, name, contractType, contractBinding, discountType, promotionGroup, promotionType);
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:149: error: incompatible types: A1PromotionPOKey cannot be converted to ORMObjectKey
return (A1PromotionPO) super.getObjectByPrimaryKeyObject(key);
^
C:\Work\VIPnet\a1webshop\app_core_a1\src\main\java\hr\a1\core\internal\promotion\A1PromotionPOFactory.java:178: error: incompatible types: A1PromotionPOKey cannot be converted to ORMObjectKey
return (A1PromotionPO) super.getObjectByPrimaryKeyObject(key, mode);
I hope you can help me with this problem because I really cannot see what is wrong here.
I have 2 entities: RokZaPrijavuProjekta AND Predmet.
RokZaPrijavuProjekta:
/**
* #ORM\Table(name="rok_prijava_projekta")
* #ORM\Entity(repositoryClass="JP\AdminBundle\Repository\RokZaPrijavuProjektaRepository")
*/
class RokZaPrijavuProjekta
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer $id_predmet
* #ORM\ManyToOne(targetEntity="Predmet")
* #ORM\Column(name="id_predmet", type="integer")
*/
private $predmet;
/**
* #var date $od
* #ORM\Column(name="od", type="date")
*/
private $od;
/**
* #var date $do
* #ORM\Column(name="do", type="date")
*/
private $do;
/**
* #var string $info
* #ORM\Column(name="info", type="string", length=120)
*/
private $info;
}
Predmet entity code:
/**
* #ORM\Table(name="predmeti")
* #ORM\Entity(repositoryClass="JP\AdminBundle\Repository\PredmetRepository")
*/
class Predmet
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $sifra
* #ORM\Column(name="sifra", type="string", length=64)
*/
private $sifra;
/**
* #var boolean $vidljiv
* #ORM\Column(name="vidljiv", type="boolean")
*/
private $vidljiv;
}
Repository method:
$q = $this->createQueryBuilder('r')
->select('rzpp')
->where('rzpp.predmet = :predmet')
->from('JPAdminBundle:RokZaPrijavuProjekta', 'rzpp')
->leftJoin("rzpp.predmet", "p")
->setParameter('predmet', $predmet)
->getQuery();
Both getters and setters for all class members are defined properly.
Now, "RokZaPrijavuProjekta" has a foreign-key reference to "Predmet", so many of these "RokZaPrijavuProjekta" can have the same "Predmet".
I want to create unidirectional ManyToOne relation for this purpose but keep getting exception thrown:
Class JP\AdminBundle\Entity\RokZaPrijavuProjekta has no association named predmet
I went all over Doctrine documentation, but found that this is the preferred way to define unidirectional many-to-one relation.
Do you have any idea what might be a problem here?
UPDATE
Added Predmet entity code...
Added Repository method
Thanks a lot!
Regards,
Jovan
Well, last night I encountered the same problem again. This time I took some time to figure out why it started working so suddenly last time (at the time of my question above).
So, the bottom line and solution to the problem:
In my entity class I had both #Column and #ManyToOne annotation where I used #Column to define name of column in database and #ManyToOne to define relation. The point is that you need to remove #Column annotation and replace it with #JoinColumn thus defining both name and referencedColumnName attributes. Check these snippets out:
This will not work:
(either with or without #JoinColumn)
/**
* #var integer $file
* #ORM\Column("name="id_file", type="integer")
* #ORM\ManyToOne(targetEntity="File")
*/
private $file
But this will:
/**
* #var integer $file
* #ORM\ManyToOne(targetEntity="File")
* #ORM\JoinColumn(name="id_file", referencedColumnName="id")
*/
private $file
I hope this will be helpful to someone...
Cheers!
Can you show Predmet entity code?
Or just try out this code:
// RokZaPrijavuProjekta
/**
* #ORM\ManyToOne(targetEntity="Predmet", inversedBy="rokzaprojects")
*/
protected $predmet;
//Predmet
/**
* #ORM\OneToMany(targetEntity="RokZaPrijavuProjekta", mappedBy="predmet")
*/
protected $rokzaprojects;
Additional Info, the format of the comments does matter
I wasted a few hours just to find out that i missed a asterisk sign in the comments
This won't work
/*
* #ORM\OneToMany(targetEntity="\Custom\Models\ProductText", mappedBy="product", orphanRemoval=true, cascade={"persist","remove"})
*/
protected $texts;
however this works
/**
* #ORM\OneToMany(targetEntity="\Custom\Models\ProductText", mappedBy="product", orphanRemoval=true, cascade={"persist","remove"})
*/
protected $texts;
notice the missing asterisk (*) at the first line of the first example