Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DeliveryManifestLines extends Eloquent
- {
- protected $table = 'manifests';
- public function sapDelivery()
- {
- return $this->hasOne('Delivery', 'DocNum', 'sap_delivery');
- }
- }
- class Delivery extends Eloquent
- {
- protected $connection = 'sap';
- protected $table = 'ODLN';
- protected $primaryKey = 'DocNum';
- public function deliveryManifest() {
- return $this->belongsTo('DeliveryManifest', 'DocNum', 'sap_delivery');
- }
- public function address()
- {
- return $this->hasOne('Address', 'Address', 'ShipToCode')->where('CardCode', $this->CardCode)->where('AdresType', 'S');
- }
- public function geolocation()
- {
- return $this->hasOne('GeoLocation', 'Address', 'ShipToCode')->where('CardCode', $this->CardCode)->where('AdresType', 'S')->where('Lat', '>', 0)->where('Lng', '>', 0);
- }
- }
- class Address extends Eloquent
- {
- protected $connection = 'sap';
- protected $table = 'CRD1';
- protected $primaryKey = 'Address';
- public function delivery() {
- return $this->belongsTo('Delivery', 'Address', 'ShipToCode');
- }
- }
- $deliveries = DeliveryManifestLines::with('sapDelivery')->where('manifest_date', $date))->get();
- foreach ($deliveries as $delivery) {
- $delivery->sapDelivery->load('address');
- }
- SELECT * FROM [CRD1] WHERE [CardCode] = 'P437' AND [AdresType] = 'S' AND [CRD1].[Address] IN ('The Pizza Factory (topping)')
- <?php
- namespace AppModelsEloquent;
- use AppModelsEloquentRelationsBelongsToCI;
- use AppModelsEloquentRelationsHasManyCI;
- use AppModelsEloquentRelationsHasOneCI;
- use IlluminateDatabaseEloquentModel;
- abstract class ModelCI extends Model
- {
- /**
- * Define a one-to-many relationship.
- *
- * @param string $related
- * @param string $foreignKey
- * @param string $localKey
- *
- * @return IlluminateDatabaseEloquentRelationsHasMany
- */
- public function hasManyCI($related, $foreignKey = null, $localKey = null)
- {
- $foreignKey = $foreignKey ?: $this->getForeignKey();
- $instance = new $related();
- $localKey = $localKey ?: $this->getKeyName();
- return new HasManyCI($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
- }
- /**
- * Define a one-to-one relationship.
- *
- * @param string $related
- * @param string $foreignKey
- * @param string $localKey
- * @return IlluminateDatabaseEloquentRelationsHasOne
- */
- public function hasOneCI($related, $foreignKey = null, $localKey = null)
- {
- $foreignKey = $foreignKey ?: $this->getForeignKey();
- $instance = new $related;
- $localKey = $localKey ?: $this->getKeyName();
- return new HasOneCI($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
- }
- /**
- * Define an inverse one-to-one or many relationship.
- *
- * @param string $related
- * @param string $foreignKey
- * @param string $otherKey
- * @param string $relation
- * @return IlluminateDatabaseEloquentRelationsBelongsTo
- */
- public function belongsToCI($related, $foreignKey = null, $otherKey = null, $relation = null)
- {
- // If no relation name was given, we will use this debug backtrace to extract
- // the calling method's name and use that as the relationship name as most
- // of the time this will be what we desire to use for the relationships.
- if (is_null($relation))
- {
- list(, $caller) = debug_backtrace(false, 2);
- $relation = $caller['function'];
- }
- // If no foreign key was supplied, we can use a backtrace to guess the proper
- // foreign key name by using the name of the relationship function, which
- // when combined with an "_id" should conventionally match the columns.
- if (is_null($foreignKey))
- {
- $foreignKey = snake_case($relation).'_id';
- }
- $instance = new $related;
- // Once we have the foreign key names, we'll just create a new Eloquent query
- // for the related models and returns the relationship instance which will
- // actually be responsible for retrieving and hydrating every relations.
- $query = $instance->newQuery();
- $otherKey = $otherKey ?: $instance->getKeyName();
- return new BelongsToCI($query, $this, $foreignKey, $otherKey, $relation);
- }
- }
- <?php namespace AppModelsEloquentRelations;
- use IlluminateDatabaseEloquentModel;
- use IlluminateDatabaseEloquentBuilder;
- use IlluminateDatabaseQueryExpression;
- use IlluminateDatabaseEloquentCollection;
- use IlluminateDatabaseEloquentRelationsBelongsTo;
- class BelongsToCI extends BelongsTo {
- /**
- * Match the eagerly loaded results to their parents.
- *
- * @param array $models
- * @param IlluminateDatabaseEloquentCollection $results
- * @param string $relation
- * @return array
- */
- public function match(array $models, Collection $results, $relation)
- {
- $foreign = $this->foreignKey;
- $other = $this->otherKey;
- // First we will get to build a dictionary of the child models by their primary
- // key of the relationship, then we can easily match the children back onto
- // the parents using that dictionary and the primary key of the children.
- $dictionary = array();
- foreach ($results as $result)
- {
- $dictionary[strtolower($result->getAttribute($other))] = $result;
- }
- // Once we have the dictionary constructed, we can loop through all the parents
- // and match back onto their children using these keys of the dictionary and
- // the primary key of the children to map them onto the correct instances.
- foreach ($models as $model)
- {
- if (isset($dictionary[strtolower($model->$foreign)]))
- {
- $model->setRelation($relation, $dictionary[strtolower($model->$foreign)]);
- }
- }
- return $models;
- }
- }
- <?php namespace AppModelsEloquentRelations;
- use IlluminateDatabaseEloquentCollection;
- use IlluminateDatabaseEloquentRelationsHasMany;
- class HasManyCI extends HasMany {
- /**
- * Build model dictionary keyed by the relation's foreign key.
- *
- * @param IlluminateDatabaseEloquentCollection $results
- * @return array
- */
- protected function buildDictionary(Collection $results)
- {
- $dictionary = array();
- $foreign = $this->getPlainForeignKey();
- // First we will create a dictionary of models keyed by the foreign key of the
- // relationship as this will allow us to quickly access all of the related
- // models without having to do nested looping which will be quite slow.
- foreach ($results as $result)
- {
- $dictionary[strtolower($result->{$foreign})][] = $result;
- }
- return $dictionary;
- }
- /**
- * Match the eagerly loaded results to their many parents.
- *
- * @param array $models
- * @param IlluminateDatabaseEloquentCollection $results
- * @param string $relation
- * @param string $type
- * @return array
- */
- protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
- {
- $dictionary = $this->buildDictionary($results);
- // Once we have the dictionary we can simply spin through the parent models to
- // link them up with their children using the keyed dictionary to make the
- // matching very convenient and easy work. Then we'll just return them.
- foreach ($models as $model)
- {
- $key = strtolower( $model->getAttribute($this->localKey) );
- if (isset($dictionary[$key]))
- {
- $value = $this->getRelationValue($dictionary, $key, $type);
- $model->setRelation($relation, $value);
- }
- }
- return $models;
- }
- }
- <?php namespace AppModelsEloquentRelations;
- use IlluminateDatabaseEloquentCollection;
- use IlluminateDatabaseEloquentRelationsHasOne;
- class HasOneCI extends HasOne {
- /**
- * Match the eagerly loaded results to their many parents.
- *
- * @param array $models
- * @param IlluminateDatabaseEloquentCollection $results
- * @param string $relation
- * @param string $type
- * @return array
- */
- protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
- {
- $dictionary = $this->buildDictionary($results);
- // Once we have the dictionary we can simply spin through the parent models to
- // link them up with their children using the keyed dictionary to make the
- // matching very convenient and easy work. Then we'll just return them.
- foreach ($models as $model)
- {
- $key = strtolower($model->getAttribute($this->localKey));
- if (isset($dictionary[$key]))
- {
- $value = $this->getRelationValue($dictionary, $key, $type);
- $model->setRelation($relation, $value);
- }
- }
- return $models;
- }
- /**
- * Build model dictionary keyed by the relation's foreign key.
- *
- * @param IlluminateDatabaseEloquentCollection $results
- * @return array
- */
- protected function buildDictionary(Collection $results)
- {
- $dictionary = array();
- $foreign = strtolower($this->getPlainForeignKey());
- // First we will create a dictionary of models keyed by the foreign key of the
- // relationship as this will allow us to quickly access all of the related
- // models without having to do nested looping which will be quite slow.
- foreach ($results as $result)
- {
- $dictionary[$result->{$foreign}][] = $result;
- }
- return $dictionary;
- }
- }
- $this->belongsToCI('Model');
- $this->hasManyCI('Model');
- $this->hasOneCI('Model');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement