Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
05b2e287
Commit
05b2e287
authored
Jun 13, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added ability to specify foreign key for has_one and has_many relationships.
parent
2bcf7ed3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
10 deletions
+15
-10
eloquent.php
system/db/eloquent.php
+6
-4
relate.php
system/db/eloquent/relate.php
+9
-6
No files found.
system/db/eloquent.php
View file @
05b2e287
...
...
@@ -124,22 +124,24 @@ abstract class Eloquent {
* Retrieve the query for a 1:1 relationship.
*
* @param string $model
* @param string $foreign_key
* @return mixed
*/
public
function
has_one
(
$model
)
public
function
has_one
(
$model
,
$foreign_key
=
null
)
{
return
Eloquent\Relate
::
has_one
(
$model
,
$this
);
return
Eloquent\Relate
::
has_one
(
$model
,
$
foreign_key
,
$
this
);
}
/**
* Retrieve the query for a 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @return mixed
*/
public
function
has_many
(
$model
)
public
function
has_many
(
$model
,
$foreign_key
=
null
)
{
return
Eloquent\Relate
::
has_many
(
$model
,
$this
);
return
Eloquent\Relate
::
has_many
(
$model
,
$
foreign_key
,
$
this
);
}
/**
...
...
system/db/eloquent/relate.php
View file @
05b2e287
...
...
@@ -6,38 +6,41 @@ class Relate {
* Retrieve the query for a 1:1 relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
public
static
function
has_one
(
$model
,
$eloquent
)
public
static
function
has_one
(
$model
,
$
foreign_key
,
$
eloquent
)
{
$eloquent
->
relating
=
__FUNCTION__
;
return
static
::
has_one_or_many
(
$model
,
$eloquent
);
return
static
::
has_one_or_many
(
$model
,
$
foreign_key
,
$
eloquent
);
}
/**
* Retrieve the query for a 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
public
static
function
has_many
(
$model
,
$eloquent
)
public
static
function
has_many
(
$model
,
$
foreign_key
,
$
eloquent
)
{
$eloquent
->
relating
=
__FUNCTION__
;
return
static
::
has_one_or_many
(
$model
,
$eloquent
);
return
static
::
has_one_or_many
(
$model
,
$
foreign_key
,
$
eloquent
);
}
/**
* Retrieve the query for a 1:1 or 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
private
static
function
has_one_or_many
(
$model
,
$eloquent
)
private
static
function
has_one_or_many
(
$model
,
$
foreign_key
,
$
eloquent
)
{
$eloquent
->
relating_key
=
\System\Str
::
lower
(
get_class
(
$eloquent
))
.
'_id'
;
$eloquent
->
relating_key
=
(
is_null
(
$foreign_key
))
?
\System\Str
::
lower
(
get_class
(
$eloquent
))
.
'_id'
:
$foreign_key
;
return
Factory
::
make
(
$model
)
->
where
(
$eloquent
->
relating_key
,
'='
,
$eloquent
->
id
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment