clone

Clone an Eloquent object including all relationships?

扶醉桌前 提交于 2019-11-27 11:45:35
Is there any way to easily clone an Eloquent object, including all of its relationships? For example, if I had these tables: users ( id, name, email ) roles ( id, name ) user_roles ( user_id, role_id ) In addition to creating a new row in the users table, with all columns being the same except id , it should also create a new row in the user_roles table, assigning the same role to the new user. Something like this: $user = User::find(1); $new_user = $user->clone(); Where the User model has class User extends Eloquent { public function roles() { return $this->hasMany('Role', 'user_roles'); } }

java.util.Date clone or copy to not expose internal reference

*爱你&永不变心* 提交于 2019-11-27 11:17:28
It is best practice not to expose the internal references of an Object (Entity). So if an Object has a field of type java.util.Date then for example the getter for this field should return not the original date but an copy of it. But for an java.util.Date there are two common ways to create that copy: clone: (Date) originalDate.clone() copy via constructor new Date(originalDate.getTime()) My question is, which way is better, and why? Jon Skeet If it's definitely just a Date , it won't make any difference either way. If the actual object might be a subclass of Date (such as java.sql.Date ) then

Should we use clone or BeanUtils.copyProperties and why

别说谁变了你拦得住时间么 提交于 2019-11-27 11:05:31
问题 By the looks of it - BeanUtils.copyProperties seems to create a clone of an object. If this is the case, and what with the concerns around implementing the Cloneable interface (Only immutable objects are new where as mutable objects have references copied) which is the best and why? I yesterday implemented cloneable and then realised I had to provide my own modifications for non String/Primative elements. I was then informed about BeanUtils.copyProperties which I am now using. Both

Git svn clone: How to defer fetch of revision history

狂风中的少年 提交于 2019-11-27 11:03:55
问题 I often have the case that I want to work on a SVN repository right away. But an ordinary git svn clone [url] also clones the entire history. So I want to speed things up. The first part is to fetch only the last revision into your Git repository. I do it like so: URL=http://google-web-toolkit.googlecode.com/svn/trunk/ REV=`svn info $URL |grep Revision: | awk '{print $2}'` PROJECT_FOLDER=google-web-toolkit-readonly git svn clone -r$REV:HEAD $URL $PROJECT_FOLDER (more info in the StackOverflow

How do I clone a sub-folder of a repository in Mercurial?

青春壹個敷衍的年華 提交于 2019-11-27 10:50:56
I have a Mercurial repository containing a handful of related projects. I want to branch just one of these projects to work on it elsewhere. Is cloning just part of a repository possible, and is that the right way to achieve this? Martin Geisler What you want is a narrow or partial clone , but this is unfortunately not yet supported. If you already have a big repository and you realize that it would make sense to split it into several smaller repositories, then you can use the convert extension to do a Mercurial to Mercurial conversion . Note that this creates a new repository foo and you

Java : clone() operation calling super.clone()

青春壹個敷衍的年華 提交于 2019-11-27 10:46:38
问题 I am not fully understanding the idea of returning super.clone() in the clone() method of a class. First of all, wouldn't that relate to it returning an object that is a superclass which contains LESS data than requested, because a superclass "is not a" subclass, but a subclass "is a" superclass. And if there were a long chain of subclasses, each calling super.clone(), why wouldn't that lead to it eventually calling Object.clone() at the root of the chain, which isn't any of the subclasses?

What is the method MemberwiseClone() doing?

早过忘川 提交于 2019-11-27 10:42:15
问题 I am confused with this code below, Developer devCopy = (Developer)dev.Clone(); Clone method of Developer class just creating a Employee clone, then how developer get another clone of developer. public abstract class Employee { public abstract Employee Clone(); public string Name { get; set; } public string Role { get; set; } } public class Typist : Employee { public int WordsPerMinute { get; set; } public override Employee Clone() { return (Employee)MemberwiseClone(); } public override

Shallow copy of a Map in Java

℡╲_俬逩灬. 提交于 2019-11-27 10:37:37
问题 As I understand it, there are a couple of ways (maybe others as well) to create a shallow copy of a Map in Java: Map<String, Object> data = new HashMap<String, Object>(); Map<String, Object> shallowCopy; // first way shallowCopy = new HashMap<String, Object>(data); // second way shallowCopy = (Map<String, Object>) ((HashMap<String, Object>) data).clone(); Is one way preferred over the other, and if so, why? One thing worth mentioning is that the second way gives an "Unchecked Cast" warning.

git: fatal: I don't handle protocol '​​http'

一笑奈何 提交于 2019-11-27 10:08:43
I copy and pasted an git clone command from a web page: https://fedorahosted.org/ibus-typing-booster/ I got this: user@host> git clone ​​http://git.fedorahosted.org/git/ibus-typing-booster.git Cloning into 'ibus-typing-booster'... fatal: I don't handle protocol '​​http' I copied and pasted the whole line git clone http://... . The character between git clone and http://... looks like a space, but it is a special Unicode character ! Short answer: After removing this character, and entering a real space, it worked! For people who love details: I see two ways to reveal ascii vs special-unicode

How does clone work under the hood?

筅森魡賤 提交于 2019-11-27 09:43:52
Clone does not call the object constructor to create a copy of the object. So what algorithm does clone use ? I am looking for implementation details of the native method clone. Any pointers will be appreciated. Note that I am aware of the shortcomings of clone. Bozho protected native Object clone() . I don't know exactly (I need to take a look at the native code) but it makes a new instance of the object inside the JVM and copies all fields. But you should avoid using clone() - it is hard to get it right. Look at this question for more details How it works is laid out in the Javadoc : The